244

I have recently been thinking about the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]

Is there a difference?

Hamid Pourjam
  • 18,954
  • 8
  • 53
  • 67
mslot
  • 4,317
  • 8
  • 30
  • 62

26 Answers26

300

They are semantically identical. The int array[] syntax was only added to help C programmers get used to java.

int[] array is much preferable, and less confusing.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
skaffman
  • 381,978
  • 94
  • 789
  • 754
  • 61
    The [] is part of the TYPE, not of the NAME. For me, that's the biggest difference. – André Chalella Sep 24 '08 at 22:50
  • 7
    @Andre - and in C, the [] its part of the declarator, not the declaration specifier, hence the `int array[]` syntax. Also logical... in some twisted way. :) – Kos Dec 08 '10 at 16:08
  • 2
    C plays this little game :) And i find it charming :) Even with like ... pointers. The 'correct' c syntax for pointers is int *imAPointer. It's silly and neat. – ScarletAmaranth Nov 30 '11 at 22:08
  • 5
    `int array[]` makes more sense to me. What do you think about this declaration? `int[] x, y`? Is `y` an array or not? Maybe it is, maybe it isn't. Only Java's gurus can answer with confidence.... – user1508893 Feb 18 '13 at 04:12
  • 26
    In case of `int[] x, y`, `y` is an array (because `[]` belongs to the type), and in case of `int x[], y`, `y` is not an array (`[]` belongs to the variable). – Triang3l Jul 07 '13 at 16:49
  • @ScarletAmaranth Nope. With C and pointers/* the * can be adjacent to either the type or the variable(not just the variable as you seem to claim). In C you can say `int*imAPointer` or `int* imAPointer` And in C, where you can say `char *a` and `char* a` both are true.. because with `char *a` *a is of type char. And with `char* a` a is of type char*. Though indeed in C with arrays you only have the square brackets after the variable name. In Java either. And in C# only after the type. – barlop Feb 21 '16 at 01:56
  • Is there a way to declare multiple arrays of the same size and type (like done here with java) in C ? eg: `int[10] a, b;` would mean integer arrays `a` and `b` each of size 10. – J...S Feb 18 '17 at 17:59
  • I acknowledge that this is a bit old thread, but I want to say, that I'm not sure about C, but I find this answer simply wrong in case of Java. Yes, most of the times, `int[] arr;` and `int arr[]` can be used interchangeably, but they are not quite same. I answer this question [here](https://stackoverflow.com/a/64064159/1553537), from where I saw the reference to this answer.. which, again, I find not quite true. – Giorgi Tsiklauri Sep 25 '20 at 12:34
181

There is one slight difference, if you happen to declare more than one variable in the same declaration:

int[] a, b;  // Both a and b are arrays of type int
int c[], d;  // WARNING: c is an array, but d is just a regular int

Note that this is bad coding style, although the compiler will almost certainly catch your error the moment you try to use d.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
58

There is no difference.

I prefer the type[] name format at is is clear that the variable is an array (less looking around to find out what it is).

EDIT:

Oh wait there is a difference (I forgot because I never declare more than one variable at a time):

int[] foo, bar; // both are arrays
int foo[], bar; // foo is an array, bar is an int.
ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
TofuBeer
  • 58,140
  • 15
  • 111
  • 160
30

No, these are the same. However

byte[] rowvector, colvector, matrix[];

is equivalent to:

byte rowvector[], colvector[], matrix[][];

Taken from Java Specification. That means that

int a[],b;
int[] a,b;

are different. I would not recommend either of these multiple declarations. Easiest to read would (probably) be:

int[] a;
int[] b;
Uddhav Gautam
  • 6,052
  • 3
  • 39
  • 54
Ishtar
  • 11,121
  • 1
  • 21
  • 30
  • 3
    It is good practice however to use one declaration per identifier instead of declaring multiple identifiers in one line. – rsp Oct 02 '10 at 14:34
  • @rsp - Totally agree, edited a best practice in. Still, it is suggestive what is good practice. – Ishtar Oct 02 '10 at 14:55
28

From section 10.2 of the Java Language Specification:

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, as in this example:

 byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];

Personally almost all the Java code I've ever seen uses the first form, which makes more sense by keeping all the type information about the variable in one place. I wish the second form were disallowed, to be honest... but such is life...

Fortunately I don't think I've ever seen this (valid) code:

String[] rectangular[] = new String[10][10];
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Thanks, I was scratching my head over this. Strangely enough, when googling the title of the question I found nothing... – Yoav Sep 22 '11 at 22:10
  • 3
    +1 I agree about disallowing the second form. +1 too because I didn't realize you could mix 'n' match like `int[] a[];` - that's *never* going to be unclear ;-) – Bohemian Sep 22 '11 at 22:23
22

The two commands are the same thing.

You can use the syntax to declare multiple objects:

int[] arrayOne, arrayTwo; //both arrays

int arrayOne[], intOne; //one array one int 

see: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
George Strother
  • 221
  • 1
  • 3
14

No difference.

Quoting from Sun:

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, as in this example: byte[] rowvector, colvector, matrix[];

This declaration is equivalent to: byte rowvector[], colvector[], matrix[][];

Community
  • 1
  • 1
Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
12

There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.

Luke Woodward
  • 56,377
  • 16
  • 76
  • 100
12

There is no real difference; however,

double[] items = new double[10];

is preferred as it clearly indicates that the type is an array.

Aaron Maenpaa
  • 105,677
  • 10
  • 91
  • 107
8

Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:

Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

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.

Note the last paragraph.

I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
7

It is an alternative form, which was borrowed from C, upon which java is based.

As a curiosity, there are three ways to define a valid main method in java:

  • public static void main(String[] args)
  • public static void main(String args[])
  • public static void main(String... args)
Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 1
    With due respect, but does this answer's the question!!!!! Is the third form, specified, actually has to do anything, with what is being asked in the original post. First two arguments are arrays to the main method, though the third entity is a variable argument ( which is not same as an array ). If defining main method is the question, then one can add another form to define a main method too, like `public static void main ( String \u005B \u005D args )`, this is another variant too. – nIcE cOw May 30 '15 at 02:45
  • 2
    @nIcEcOw a varargs parameter, eg `String... param`, *is* an array – Bohemian May 30 '15 at 06:34
  • Just a simple doubt arises, if both are arrays, i.e. String[] and String... `, then why it is not possible to call a method like say `someMethod ( 1, 2 )` if one is defining `someMethod` as `someMethod ( int[] numbers )`. Arrays demand that the argument, must be from contiguous memory location, though `varargs`, will make an array out of the supplied arguments first. Moreover, a `varargs` can only be the final argument of a method, though the same is not true for an array. – nIcE cOw May 30 '15 at 10:33
  • 2
    @nIcEcOw but you can call a varargs with an array, ie `meth(int... a)` can be called as either `meth(1, 2)` or `meth(new int[] {1, 2})`. Varargs is syntactic sugar that turns the first version into the second version. The actual (bytecode) type of the parameter is an array type. As for having to be last, if you think about it, there's no other sane choice. – Bohemian May 30 '15 at 17:56
5

There is no difference, but Sun recommends putting it next to the type as explained here

André
  • 12,069
  • 3
  • 30
  • 44
3

The most preferred option is int[] a - because int[] is the type, and a is the name. (your 2nd option is the same as this, with misplaced space)

Functionally there is no difference between them.

Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
3

The Java Language Specification says:

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, as in this
example:

byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];

Thus they will result in exactly the same byte code.

Kdeveloper
  • 13,209
  • 11
  • 38
  • 48
2

In Java, these are simply different syntactic methods of saying the same thing.

Zach Lute
  • 571
  • 1
  • 4
  • 9
2

They're the same. One is more readable (to some) than the other.

basszero
  • 28,508
  • 9
  • 50
  • 76
2

They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.

Derek Park
  • 43,840
  • 14
  • 54
  • 73
2

There is no difference in functionality between both styles of declaration. Both declare array of int.

But int[] a keeps type information together and is more verbose so I prefer it.

YoK
  • 13,731
  • 4
  • 46
  • 66
2

Both have the same meaning. However, the existence of these variants also allows this:

int[] a, b[];

which is the same as:

int[] a;
int[][] b;

However, this is horrible coding style and should never be done.

Michael Borgwardt
  • 327,225
  • 74
  • 458
  • 699
2

They are the same, but there is an important difference between these statements:

// 1.
int regular, array[];
// 2.
int[] regular, array;

in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.

The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.

Patrick
  • 16,618
  • 5
  • 65
  • 82
2

As already stated, there's no much difference (if you declare only one variable per line).

Note that SonarQube treats your second case as a minor code smell:

Array designators "[]" should be on the type, not the variable (squid:S1197)

Array designators should always be located on the type for better code readability. Otherwise, developers must look both at the type and the variable name to know whether or not a variable is an array.

Noncompliant Code Example

int matrix[][];   // Noncompliant
int[] matrix[];   // Noncompliant

Compliant Solution

int[][] matrix;   // Compliant
Robert Hume
  • 969
  • 2
  • 11
  • 22
1

Yep, exactly the same. Personally, I prefer

int[] integers; 

because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to

int integers[];

which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.

Check out this page on arrays in Java for more in depth examples.

David Watson
  • 1,924
  • 2
  • 11
  • 17
1

when declaring a single array reference, there is not much difference between them. so the following two declarations are same.

int a[];  // comfortable to programmers who migrated from C/C++
int[] a;  // standard java notation 

when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.

int a[],b[],c[]; // three array references
int[] a,b,c;  // three array references
PoornaChandra
  • 189
  • 1
  • 8
1

Yes, there's a difference.

int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address elsewhere in memory and 'a' holds only that address

int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it is integer type.

0

Both are ok. I suggest to pick one and stick with it. (I do the second one)

Albert
  • 2,055
  • 1
  • 18
  • 23
0

While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).

Kos
  • 64,918
  • 23
  • 156
  • 223