I've been trying to learn Java lately, making slow progress but I'm getting there. Anyways, I've been working my way through the book: 'Sams teach yourself Java in 24 hours' and quite early on it states:
Java is flexible about where the square brackets are placed when an array is being created. You can put them after the variable name instead of the variable type, as in the following: String niceChild[];
To make arrays easier for humans to spot in your programs, you should stick to one style rather than switching back and forth. Programs that use arrays in this book always place the brackets after the variable or object type.
I totally understand all of that, but then later on during one of the exercises it seems to go against what has just been said. As you can see below it has: String phrase[] = { but I can't help thinking it should be String[] phrase = { instead? I know that the program runs fine with either. I presume it's just a typo but I wanted to know from someone who understands it a bit better.
package com.java24hours;
class Wheel {
public static void main(String[] arguments) {
String phrase[] = {
"A STITCH IN TIME SAVES NINE",
"DON'T EAT YELLOW SNOW",
"JUST DO IT",
"EVERY GOOD BOY DOES FINE",
"I WANT MY MTV",
"I LIKE IKE",
"PLAY IT AGAIN, SAM",
"FROSTY THE SNOWMAN",
"ONE MORE FOR THE ROAD",
"HOME FIELD ADVANTAGE",
"VALENTINE'S DAY MASSACRE",
"GROVER CLEVELAND OHIO",
"SPAGHETTI WESTERN",
"AQUA TEEN HUNGER FORCE",
"IT'S A WONDERFUL LIFE"
};
int[] letterCount = new int[26];
for (int count = 0; count < phrase.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
for (int count2 = 0; count2 < letters.length; count2++) {
char lett = letters[count2];
if ( (lett >= 'A') & (lett <= 'Z') ) {
letterCount[lett - 'A']++;
}
}
}
for (char count = 'A'; count <= 'Z'; count++) {
System.out.print(count + ": " +
letterCount[count - 'A'] +
" ");
if (count == 'M') {
System.out.println();
}
}
System.out.println();
}
}