Java Reference
In-Depth Information
circumferenceofacircle.Oncedefined,thisvaluecanberecalledany
timethatitisneeded.
Java variable types
Javaisa stronglytyped language.Thismeansthateveryvariablemusthave
adeclaredtype,andthatthelanguagestrictlyenforcestype-checking.
EightprimitivedatatypesarebuiltintotheJavalanguage.Fourofthese
primitivetypes,calledintegertypes,areusedtostorewholenumbers.They
arenamed byte,short,int ,and long .Twoprimitivetypesareusedforstor-
ingdecimalnumbers.Thesetypesarenamed float and double .Thereisalso
onecharactertype,named char ,andonebooleantype,named boolean . Ta-
ble4-1 liststheJavaprimitivedatatypes.
Table4-1
JavaPrimitiveDataTypes
TYPENAME
STORAGE
APPROXIMATE
SPACE
RANGE
INTEGRALS:
int
4bytes
+/-2billion
short
2bytes
+/-32,767
long
8bytes
+/-9.2x1018
byte
1byte
-129to127
FLOATING-POINT:
float
4bytes
7-8digits
double
8bytes
16-17digits
CHARACTERTYPE:
char
2bytes
65,536
BOOLEANTYPE:
boolean
trueandfalse
Declaring a variable
BeforeyoucanuseavariableinaJavaprogramyoumustfirstdeclareitby
specifyingitstypeandassigningtoitaname.Thevariablenamemustbea
legalJavaidentifierandthevariabletypemustbeoneoftheprimitivedata
typeslistedin Table4-1 . Thepurposeofthedeclarationstatementistoin-
form the compiler of our intention of creating a variable of a specific type
and tagging it with a particular name. The result is that Java reserves a
memory space in which to store the data and associates it with the identi-
fier that we assigned as a name. For example, the following declaration cre-
ates a variable of type int:
int age1;
 
Search WWH ::




Custom Search