0

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();
    }
}
Lee
  • 1
  • I would say it's just a typo. – Roger Gustavsson Sep 15 '15 at 12:35
  • I have no idea how I managed to retract my duplicate close vote, but it is definitely a typo. – Andy Brown Sep 15 '15 at 12:36
  • Thanks guys! Unbelievably quick response aswell! Much appreciated. I had a feeling it was just a typo and I know it doesn't make a big difference but when you don't fully understand it, it looks like there could be another reason for it. I just wanted to be sure! :) – Lee Sep 15 '15 at 12:43
  • Note that his initial advice should not be followed. By convention, you *always* put the brackets after the type name. This is recommended by both [Oracle](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) (look under "Declaring a Variable to Refer to an Array") and [Google](https://google.github.io/styleguide/javaguide.html#s4.8.3-arrays). – RealSkeptic Sep 15 '15 at 12:50
  • Thanks, that Oracle link explained it well enough. I was aware that both worked but I thought there might be another reason to it. Thanks again. – Lee Sep 15 '15 at 13:56

2 Answers2

1

You can actually do:

String[] names = { "James", "Bond"}; //This is recommended

String names[] = { "James", "Bond"}; //This is legal but less readable.

CODI
  • 147
  • 4
  • 17
0

No, it's not a typo. You are reading book that try to teach you. It states:

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.

You should stick to one style, but this book would not (or not always will do like this) so you will better remember that both ways are correct. So author show you code like

...
String phrase[] = { ...
...
int[] letterCount =  ...
...

Try to look at the examples of question from the java certification exams and you will understand.

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • I understand that both will work, but because he states that it is good practice to stick to one style, I couldn't understand why he was using both. It just seemed like it was either an error, or that there was something else to it that I hadn't understood. But I understand it now. Thanks for the feeback. – Lee Sep 15 '15 at 13:57