Skip to content
Advertisement

Is the String args[] parameter needed for programs that do not require command line arguments?

As far as I know, String args[] accepts an array of elements of type String – a mechanism through which the runtime system passes information to an application.

If we take a simple addition program like this:

class Add {

    public static void main(String args[]) {
        int x = 10;
        int y = 30;
        int c = x + y;
        System.out.println(c);
    }
}

It is obvious that the program does not need any command line arguments to calculate the result. No values are passed to the args array. So, is it necessary to include this array or does the main() syntax requires us to do otherwise?

Advertisement

Answer

You are required to have a function called public static void main(String[] args) as an entry point to your Java program.

If you look at the documentation for the actual java command, it makes it explicit:

The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class’s main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration has the following form:

public static void main(String[] args)

There is a difference between this and a JavaFX program; its initial entry point is located in the start() function.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement