• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command Line Arguments

 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found it very confusing that the command line arguments do not appear to follow the rules for strings. For example,
args.length() is not valid. I believe this would be valid for a string declared as a variable.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command line arguments follow the rules for an array of Strings, because that's what they are. An array has a length field and not a length() method, while a String has a length() method but not a length field. (Why this is I don't know, but we're stuck with it.) Assuming you've declared you main method thus:
<code><pre> public static void main(String[] args) {</pre></code>
...then args refers to the whole array, and args[0] refers to the first element in the array, which is a String. So, if you want to know how many command-line arguments there are, use args.length. To find the length of the first argument (assuming it exists), use args[0].length(). Make sense?
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Jim. Your clarifications are extremely helpful.
 
Mark Whelchel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the preferred syntax?
String[] args
or
String args[]
JJ2 seems to switch freely between the two styles.

[This message has been edited by Mark Whelchel (edited April 28, 2000).]
[This message has been edited by Mark Whelchel (edited April 28, 2000).]
 
paul wheaton
Trailboss
Pie
Posts: 21335
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer String[] args. I think it is simpler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic