Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - Array brackets after variable name [duplicate]

Tags:

java

arrays

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?

like image 950
mslot Avatar asked Nov 25 '22 09:11

mslot


2 Answers

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.

like image 118
skaffman Avatar answered Jun 11 '23 18:06

skaffman


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.

like image 25
Adam Rosenfield Avatar answered Jun 11 '23 19:06

Adam Rosenfield