8
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[]?

Raedwald
  • 43,666
  • 36
  • 142
  • 227
user3572544
  • 189
  • 6

5 Answers5

28

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.

PandaConda
  • 3,266
  • 2
  • 19
  • 22
3

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.

Sanjay Rabari
  • 2,055
  • 1
  • 16
  • 29
3

Well as we already have many answers I just want to add something

//Valid declarations
String[]array;//With No Space
String[] a;
String []b;//<-----
String c[];
String[] d,e,f;
String g[],h[],i[];
String[] []y;
String[] []z[];
String []p[];
etc.

String v[],[]w;<----Not Allowed
akash
  • 22,224
  • 10
  • 57
  • 85
2

Yes, it's exactly the same. You can write public static void main(String[] args) or public static void main(String args[]) for your main method (or anything else of course). It's a good idea to standardize on whichever makes more sense to you (most likely String[]). The choice is yours.

Nateowami
  • 934
  • 15
  • 23
1

yes both are same. The second one makes more sense as it reads A String array s directly.

TheLostMind
  • 35,468
  • 12
  • 66
  • 99
  • Well, you *could* say "`s` is a `String array`", which makes sense, while "`s array` is a `String` (huh?)", but that really is a contrived example and in the end it still comes down to personal preference, and in the end you'll still use `s` by itself... – awksp Jun 16 '14 at 08:58