How to Read Program Arguments in Java

In Java like in e.g. C the program options are part of the main method signature.

public static void main (String[] args)

We can write the hello world program like this:

public class Hello {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println("Hello " + s);
        }
    }
}

Compile the program using javac Hello.java and run the program.

java Hello one two "Claus Witt"

Resulting in the following output:

Hello one
Hello two
Hello Claus Witt

Related Posts