tags:

views:

11186

answers:

5

I'm very beginner for programming.

In Java.

public static void main(String []args)

What's String args? and What kind of case do you use args at? I am fortunate when I have you teach it with source codes and examples.

+1  A: 

args contains the command-line arguments passed to the Java program upon invocation. For example, if I invoke the program like so:

$ java MyProg -f file.txt

Then args will be an array containing the strings "-f" and "file.txt".

mipadi
+23  A: 

Those are for command-line arguments in Java.

In other words, if you run

> java MyProgram one two

Then args contains:

[ "one", "two" ]
Daniel Lew
+2  A: 

String [] args is also how you declare an array of Strings in Java.

In this method signature, the array args will be filled with values when the method is called (as the other examples here show). Since you're learning though, it's worth understanding that this args array is just like if you created one yourself in a method, as in this:

public void foo() {
    String [] args = new String[2];
    args[0] = "hello";
    args[1] = "every";

    System.out.println("Output: " + args[0] + args[1]);

    // etc... the usage of 'args' here and in the main method is identical
}
Cuga
+1  A: 

When a java class is executed from the console, the main method is what is called. In order for this to happen, the definition of this main method must be

public static void main(String [])

The fact that this string array is called args is a standard convention, but not strictly required. You would populate this array at the command line whne you invoke your program

java MyClass a b c

These are commonly used to define options of your program, for example files to write to or read from.

shsteimer
+3  A: 

Those are for command-line arguments in Java.

In other words, if you run

java MyProgram one two

Then args contains:

[ "one", "two" ]

public static void main(String [] args) {
   String one = args[0]; //=="one"
   String two = args[1]; //=="two"
}

The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.

If you are new to java a highly recommend reading through the sun tutorials

mR_fr0g