Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String args[] vs. String[] args

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes I see:



But sometimes I see:



How come? What is the difference... or maybe I should ask, why two ways?

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just posted some of my notes on this in this thread. Also see JLS - 10.2 Array Variables for more detail.

Note: Many people prefer the brackets next to the type rather than the identifier (that is, String[] s instead of String s[]) because it's more clear that the type is a String array.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stricly personal preference
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
to add in, String[] args type of declaratio causes all the variable on the right side to be arrays but String args[] does not.



Hope this helps
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As people have already said, it makes no difference to the behaviour, except in the case of declaring multiple variables in one line. And, frankly, if you declare some Strings and some arrays of Strings, all in one line, you deserve to be forced to write Visual Basic for the rest of your career.

I think the "String args[]" version is one of the features of the Java language that was put in to help to make C programmers feel a bit more at home. It deserves to be ignored by all right-thinking Java programmers.

Just my humble opinion of course ;-)
[ July 03, 2007: Message edited by: Peter Chase ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could hardly agree with you more, Peter Chase. It gets confusing when people writeOnly digits is an array. Even worse isThere everything is a [] array except digits which is a [][] array, but it is very easy to overlook that feature in a quick glance at the code.
 
Dawei Li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String args[] is old definition
String[] args is new definition.
The both have same function.
But the java recommends the new.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic