Is there any difference between between these two array declaration syntax in java?
int[] variableName;
and
int variableName[];
Which one is preferred?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Although both syntaxes are equivalent, the
int[] variableName;syntax is preferred. Theint variableName[];syntax is allowed just for making comfortable all the C/C++ programmers transitioning to Java.One could argue that
int[] xclearly states that integer array is the type of x, whereas inint x[]the actual type is split in two parts, one before and the other after thexvariable, reading like integer is the type of the variable x which is an array making the declaration less readable and potentially confusing to a newcomer.The readability problem is exacerbated if the array has more dimensions, for example all of these declarations are equivalent and valid:
Also notice that the same considerations apply to all these valid, equivalent method declarations (the return type is the same in all cases) – but once again the last one is simpler to read: