Java Reference
In-Depth Information
semicolon character ( ; ). Consider the following examples:
int counter;
double temperature;
String firstName;
int[] ages;
char gradeLetters[];
float[][] matrix;
Thefirstexampledeclaresanintegervariablenamed counter ,thesecondexample
declares a double precision floating-point variable named temperature , the third
example declares a string variable named firstName , the fourth example declares
a one-dimensional integer array variable named ages , the fifth example declares a
one-dimensional character array variable named gradeLetters , and the sixth ex-
ample declares a two-dimensional floating-point array variable named matrix . No
stringisyetassociatedwith firstName ,andnoarraysareyetassociatedwith ages ,
gradeLetters , and matrix .
Caution Squarebracketscanappearafterthetypenameorafterthevariablename,
but not in both places. For example, the compiler reports an error when it encounters
int[] x[]; .Itiscommonpracticetoplacethesquarebracketsafterthetypename
(asin int[] ages; )insteadofafterthevariablename(asin char gradeLet-
ters[]; ).
You can declare multiple variables on one line by separating each variable from its
predecessor with a comma, as demonstrated by the following example:
int x, y[], z;
This example declares three variables named x , y , and z . Each variable shares the
sametype,whichhappenstobeinteger.Unlike x and z ,whichstoresingleintegerval-
ues, y[] signifiesaone-dimensionalarraywhoseelementtypeisinteger-eachelement
stores an integer value. No array is yet associated with y .
name,asin int x, []y, z; ,thecompilerreportsanerror.Ifyouplacethesquare
bracketsafterthetypename,asin int[] x, y, z; ,allthreevariablessignifyone-
dimensional arrays of integers.
Search WWH ::

Custom Search