2

Working in Java, what is the difference between using

Object[] variableName;

and using:

Object variableName[];

Does it have the exact same effect on compilation and run? Or is there a difference?

Matt Clark
  • 27,671
  • 19
  • 68
  • 123

3 Answers3

4

Both statements are entirely equivalent.

wvdz
  • 16,251
  • 4
  • 53
  • 90
2

The statements will compile to the same code, BUT if you write

Type[] name instead of Type name[] the code becomes more readable, because you always can see the type (Array or Not-Array) in front of the variable name. (In fact this is some kind of my ppersonal meaning)

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
1

From Java language specification (for Java 7) :

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.

So yes, they are both equivalent and you can even mix the two styles in the same declaration (although the specification gives a healthy reminder to us that that tends to get ugly and confusing).

kviiri
  • 3,282
  • 1
  • 21
  • 30