3

i wrote this code in java and i was thinking about what's the difference between making this: String [] whatever and String whatever [], if someone could told me what's happening, it's better use String [] whatever = {} or use String whatever[] = {} or this it's just deprecated.

public class Snippet136 
{
    public static void main(String [] args) 
    {
        String names[] = {"Jhonny", "Edurardo", "Francis", "Franklin", "Freedy"};
        String [] eyes_colors = { "blue", "red", "black", "green", "black"};

        System.out.print("Names:");
        for (String name: names) {
            System.out.print( " " + name );
        }

        System.out.print("\n\nEyes colors:");
        for ( String eyes_color: eyes_colors ) {
            System.out.print( " " + eyes_color );
        }
    }
}
xsami
  • 1,312
  • 16
  • 31
  • possible duplicate of [Difference between int\[\] array and int array\[\]](http://stackoverflow.com/questions/129178/difference-between-int-array-and-int-array) – Reimeus Feb 19 '15 at 00:53

2 Answers2

6

Nothing whatsoever: the two forms are identical.

String[] var is more natural, and generally preferred (since it more logically combines the array type with the type name). String var[] is in Java basically because C/C++ use that syntax, and so it is a kind of "syntax compatibility".

Note that the postfix [] leads to some really insane syntax features, such as the ability to write int function()[] { return null; }: a function returning an int[].

nneonneo
  • 171,345
  • 36
  • 312
  • 383
6

Yes, there is difference:

Only arrays:

String[] arr1, arr2;

Not just arrays:

String arr1[], str1, arr2[], str2;