Sunday, 14 August 2016

Anirudh

Command line arguments in Java

Command line arguments constitute the additional information that can be passed to a program at the time of its execution via command line. They are given just after the program's name on the command line interface of the operating system.

You can read more about the use of command line arguments here.

Command line arguments in Java:
The arguments passed from the console can be received in a Java program via the argument variable in its main function. To use command line arguments in your Java program, you must first understand the declaration of the main function. There is an array of Strings present within the declaration of the main method, generally named as String[] args (since args is nothing more than an identifier, we can replace it with any other name). Now what we need to know is how a String array can be passed as an argument when we execute the program. We pass it through the command line itself.

Consider that we have a class named Add. The following statement is normally used to execute the program: java Add
If we wish to pass the String array, we simply write the elements of the array after the class name. Enclosing the Strings in quotes is optional. Consecutive Strings are separated with a space. For example, if we wish to pass two Strings as arguments, containing the values "15" and "3", then any of the following lines can be entered on the command prompt: java Add 15 3 or java Add "15" "3"

Since these arguments are passed through the command line, they are known as command line arguments. The String arguments passed are stored in the array of Strings specified in the main function's header. In the above example, args[] will contain two Strings, "15" and "3". These elements are accessed in the same way as the elements of a normal array. Note that unlike C, Java does not store the program file's name as a command line argument. The following is a complete program which displays the sum of numbers passed as command line arguments.

/* Java program to display sum of all given numbers via command line arguments: */

public class Add {

    public static void main (String[] args) {
        int sum = 0;

        if (args.length == 0)
            System.out.println ("No arguments provided.");
        else {
            for (int i = 0; i < args.length; i++)
                sum += Integer.parseInt (args[i]);    // Convert string args[i] to integer

            System.out.println ("Sum of provided numbers: " + sum);
        }
    }
}
The above program works like this:
java Add
No arguments provided.

java Add 15 3
Sum of provided numbers: 18

java Add "15" "3"
Sum of provided numbers: 18

Anirudh

About the author →

Anirudh Khanna is a Computer Science student and a geek who firmly believes in the awesomeness of technology! He is a programmer and web designer, also keenly interested in the research of new and innovative ideas in Computer Science.

Subscribe to Geek Factorial via email :