Java Reference
In-Depth Information
Note Developers who argue that Java should only support objects are not happy
abouttheinclusionofprimitivetypesinthelanguage.However,Javawasdesignedto
includeprimitivetypestoovercomethespeedandmemorylimitationsofearly1990s-
era devices, to which Java was originally targeted.
User-Defined Types
A user-defined type isatypethatisdefinedbythedeveloperusingaclass,aninterface,
an enum, or an annotation type; and whose values are objects. For example, Java's
String class defines the string user-defined type; its values describe strings of char-
acters, and its methods perform various string operations such as concatenating two
strings together. Chapter 2 discusses classes, interfaces, and methods. Chapter 3 dis-
cusses enums and annotation types.
User-definedtypesarealsoknownas reference types becauseavariableofthattype
stores a reference (a memory address or some other identifier) to a region of memory
thatstoresanobjectofthattype.Incontrast,variablesofprimitivetypesstorethevalues
directly; they don't store references to these values.
Array Types
An array type isaspecialreferencetypethatsignifiesan array ,aregionofmemorythat
storesvaluesinequal-sizeandcontiguousslots,whicharecommonlyreferredtoas ele-
ments .
This type consists of the element type (a primitive type or a user-defined type) and
one or more pairs of square brackets that indicate the number of dimensions (extents).
A single pair of brackets signifies a one-dimensional array (a vector), two pairs of
bracketssignifyatwo-dimensionalarray(atable),threepairsofbracketssignifyaone-
dimensional array of two-dimensional arrays (a vector of tables), and so on. For ex-
ample, int[] signifies a one-dimensional array (with int as the element type), and
double[][] signifies a two-dimensional array (with double as the element type).
Variables
Programs manipulate values that are stored in memory, which is symbolically repres-
ented in source code through the use of the variables feature. A variable is a named
memorylocationthatstoressometypeofvalue.Variablesthatstorereferencesareoften
referred to as reference variables .
Variablesmustbedeclaredbeforetheyareused.Adeclarationminimallyconsistsof
atypename,optionallyfollowedbyasequenceofsquarebracketpairs,followedbya
Search WWH ::




Custom Search