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 [ ] args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String [ ] args" Watch "String [ ] args" New topic

String [ ] args

Parth Sagdeo
Ranch Hand

Joined: Mar 18, 2004
Posts: 40
What is that, and what does it do?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
It is how you access the arguments passed to your program from the java command.

"java MyProgram alpha beta gamma" will pass your MyProgram.main() method a 3 element array of strings looking like this:
String[] args = { "alpha", "beta", "gamma" };

Mike Gershman
SCJP 1.4, SCWCD in process
marc weber

Joined: Aug 31, 2004
Posts: 11343

A String array is the argument type to the main method used as a Java entry point:

public static void main(String[] args){ ... }

Any legal identifier can be used for this array, although "args" or "argv" are probably the most common (for "arguments" and "argument values" respectively).

The general purpose of this array is a means to pass initial information into a program. When running from the Command Line, arguments can be added after the file name, separated by spaces. For example, "c:\>java FileName Zero One" will invoke the main method of FileName.class, with "Zero" and "One" as elements of the String array passed to that method.

Try compiling the following code, then run with the following commands:
  • java MainArgs
  • java MainArgs what is this

  • Note that args (or whatever you might call it) is like any other argument, in that the reference to the String array is local to the main method; so it's legal (although not advisable) to define another String[] called "args" elsewhere in the class. Also note that a main method can be called like any other method -- it is not restricted to being invoked from the Command Line.
    [ January 03, 2005: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    I agree. Here's the link:
    subject: String [ ] args
    It's not a secret anymore!