4 minutes read

As you already know, the main method is an entry point of every Java program.

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

As it works as a usual method, it can receive some parameters via the String[] args part. But as it is the main method of the program, you can think of them as the whole program's arguments, so-called command-line arguments.

So, here we will look closer at how these arguments are used.

Different numbers of parameters + passing example

There are different ways of providing command-line args to a program. For example, you can define them with your IDE tools or write them manually as a program run command parameters:

java Main myArg

We can pass a different number of parameters separating them with space and get the values in main(...) as array members:

public class Main {
    public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);
    }
}

Now if we compile the Main class from its directory with

javac Main.java 

and then call it with two arguments we will get the expected output:

java Main myFirstArg mySecondArg
myFirstArg
mySecondArg

If we want to pass an argument that consists of multiple "words" separated by blank space, we must put such an argument in double quotes:

java Main myFirstArg "multiple word arg"
myFirstArg
multiple word arg

We don't have to pass any arguments, but the program will fail while trying to get an argument that was not provided:

java Main
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at Main.main(Main.java:5)

Argument types

The command-line arguments are always provided as an array of Strings. You can still pass number values, but they will be accepted as Strings. Casting to a number or whatever type you expect it to be should be done manually in the program:

public class Main {
    public static void main(String[] args) {
        int i = Integer.parseInt(args[0]);
        System.out.println("Provided number : " + i);
    }
}

We are using standard output tools here to simplify the example. It is not the common way of logging/showing error information in real applications.

Now we need to recompile the class with javac.

Then, if we test it with a correct integer value, it looks like:

java Main 5
Provided number : 5

With or without arguments?

What will happen, if there are two different main methods: with and without args?

public class Main {
    
    public static void main(String[] args) {
        System.out.println("Method with arguments called!");
        System.out.println(args[0]);
    }

    public static void main() {
        System.out.println("No arguments method called!");
    }
    
}

If you recompile and try to call the program with empty arguments list and then with any argument, the first method will be called both times.

It means, that in Java the main method always has to accept arguments, but you don't have to always provide them.

So, if your program requires no arguments, its main method still has to have String[] args as parameter. Java programmers are not allowed to change the type of args array as well: it has to be String[].

Summary

Now you know more about command-line arguments in Java. You can pass them to your program as Strings and handle them in the main method to make your application more flexible and configurable.

It's time to revise what you have learned with some tasks!

590 learners liked this piece of theory. 43 didn't like it. What about you?
Report a typo