Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array syntax in Java: what is the significance of the [] location [duplicate]

String S[] = new String[3];
String[] S = new String[3];

Both ways are proper in Java. Does it mean that for every type Type[] x is the same as Type x[]?

like image 652
user3572544 Avatar asked Jun 16 '14 08:06

user3572544


2 Answers

The difference is if you declare multiple variables on the same line:

String A[], B, C;

would result in A being an array of Strings, whereas B and C are Strings.

String[] A, B, C;

would result in A, B and C all being arrays of Strings.

like image 127
PandaConda Avatar answered Sep 24 '22 13:09

PandaConda


yes both are same. but

String[] s; is more readable because If you have thousand line of code some time

you may read String s[] like String s;

but in String[] s you will read as proper String array.


Also if you declare multiple varialbes in one line

like String[] x,y,z;

and String x[],y,z; you can read the difference easily.

like image 43
Sanjay Rabari Avatar answered Sep 22 '22 13:09

Sanjay Rabari