Variables in Java

Objectives

Variable Declaration

Variable Assignment

Variable Initialization

Primitive or Reference

Primitive Types

Type Purpose Size (bytes) Examples
boolean Store true or false 1 boolean b = true;
char Store a single unicode character 2 char c = 'A';
byte Store a number from this range [-27, 27) = [-128, 128) = [-128, 127] 1 byte b = 12;
short [-215, 215) = [-32768, 32768) = [-32768, 32767] 2 short s = -25000;
int [-231, 231) = [-2147483648, 2147483648) 4 int i = 4013;
long [-263, 263) = [-9223372036854775808, 9223372036854775808) 8 long n = 3;
float Store a floating point number (a number with digits after the decimal separator) with approx. 7 decimal digits of precision 4 float f = 4.15f;
double More precise than float, approx. 15 decimal digits of precision 8 double d = 76.178032;

Automatic Promotion of Primitive Variables

Type Casting Primitive Variables


Copyright © 2010, Maia L.L.C.  All rights reserved.