0

If I declare an array of objects like one of the following.

MyClass[ ] myArray;

MyClass myArray[ ];

Are both right?

Community
  • 1
  • 1
Castro
  • 1
  • 1
  • @Jeroen Is [that question](http://stackoverflow.com/questions/1200621/declare-array-in-java) really a [duplicate](http://stackoverflow.com/help/duplicates) of this? – Elliott Frisch Mar 19 '15 at 01:49
  • I don't see any reason why it wouldn't be. – Jeroen Vannevel Mar 19 '15 at 01:51
  • @Jeroen I read this question as *is one array declaration the same as an apparently different array declaration?* I read that question as *how do I declare an array?* At the very least, the [top-rated (and accepted) answer](http://stackoverflow.com/a/1200646/2970947) doesn't answer this question at all. There is [an answer](http://stackoverflow.com/a/1200648/2970947) that might imply an answer to this question, but it isn't exactly direct. – Elliott Frisch Mar 19 '15 at 01:57
  • @ElliottFrisch: it's a canonical Q+A about array declaration and 5 other high voted answers explicitly talk about this, to me that is a certain duplicate. – Jeroen Vannevel Mar 19 '15 at 02:00

1 Answers1

2

Both expressions are equivalent, of note is that

Object[] arr;
Object arr[];

create identical bytecode (in Java the first is usually preferred as it is clearer when reading that it's an array). JLS-10.2. Array Variables says (in part)

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226