Is there any difference between "Object[] x" and "Object x[]"?
arrays, declaration, java
Solution
Both are legal and both work. But placing [] before the array's name is recommended.
From Javadocs:
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Problem
I was updating a legacy code base in Java and I found a line like this: ``` Object arg[] = { new Integer(20), new Integer(22) }; ``` That line catched my attention because I am used to this kind of code: ``` Object[] arg = { new Integer(20), new Integer(22) }; ``` The content of the array isn't important here. I'm curious about the brackets next to the variable name versus the brackets next to the class name. I tried in Eclipse (with Java 5) and both lines are valid for the compiler. Is there any difference between those declarations?