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
  • 26,491
  • 18
  • 65
  • 118

3 Answers3

4

Both statements are entirely equivalent.

wvdz
  • 16,053
  • 4
  • 47
  • 86
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,550
  • 4
  • 42
  • 78
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,182
  • 1
  • 20
  • 30