What is the “String args[]” parameter in the main method?

In Java args contains the supplied command-line arguments as an array of String objects.

In other words, if you run your program in your terminal as :

C:/ java MyProgram one two

then args will contain ["one", "two"].

If you wanted to output the contents of args, you can just loop through them like this…

public class ArgumentExample {
    public static void main(String[] args) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}

The program will print in the terminal:

C:/ java MyProgram one two
one
two
    
C:/

Leave a Comment