File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String[] a vs String word[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String[] a vs String word[]" Watch "String[] a vs String word[]" New topic

String[] a vs String word[]

M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 41
Verify simple question.
What is the difference between declaring a variable String[] aString vs. String aString[]?
Thank you very much.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
No difference.

You just prefer to write String[] astring version, because in this way is more readable, it's more easy to see the type of the reference astring

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955


When i see "String [] allNames" I mentally parse each part in order, as "a string array called allNames".

When I see " String allNames[]", I mentally parse it as "a string called allNames - OH WAIT!!! It's an ARRAY!!!".

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
The reason you mentally parse it like that, Fred, is that the first version correctly gives the type of the variable as an array of Strings, which is why it is preferred.

I think Sun ought to have prohibited foo array[] or similar, but wimped out of scaring off old C/C++ developers.
I agree. Here's the link:
subject: String[] a vs String word[]
It's not a secret anymore!