Java Reference
In-Depth Information
used to count 'b' characters, and so on. A separate variable called other is used
to count any nonalphabetic characters that are encountered.
Note that to determine if a character is an uppercase letter we used the bool-
ean expression (current >= 'A' && current <= 'Z') . A similar expression is
used for determining the lowercase letters. We could have used the static methods
isUpperCase and isLowerCase in the Character class to make these determina-
tions but didn't in this example to drive home the point that because characters
are based on the Unicode character set, they have a specific numeric value and
order that we can use in our programming.
We use the current character to calculate which index in the array to refer-
ence. We have to be careful when calculating an index to ensure that it remains
within the bounds of the array and matches to the correct element. Remember
that in the Unicode character set, the uppercase and lowercase alphabetic
letters are continuous and in order (see Appendix C). Therefore, taking the
numeric value of an uppercase letter such as 'E' (which is 69) and subtracting
the numeric value of the character 'A' (which is 65) yields 4, which is the cor-
rect index for the counter of the character 'E' . Note that nowhere in the pro-
gram do we actually need to know the specific numeric values for each letter.
Alternate Array Syntax
Syntactically, there are two ways to declare an array reference in Java. The first
technique, which is used in the previous examples and throughout this text, is
to associate the brackets with the type of values stored in the array. The second
technique is to associate the brackets with the name of the array. Therefore, the
following two declarations are equivalent:
int [] grades;
int grades[];
Although there is no difference between these declaration techniques as far
as the compiler is concerned, the first is consistent with other types of declara-
tions. The declared type is explicit if the array brackets are associated with the
element type, especially if there are multiple variables declared on the same
line. Therefore we associate the brackets with the element type throughout this
text.
Initializer Lists
You can use an initializer list to instantiate an array and provide the initial values
for the elements of the array. It is essentially the same idea as initializing a variable of
 
Search WWH ::




Custom Search