Command Line Arguments in Java offer us a way to provide the input data for a program while the program is running. Hence these arguments enable us to conveniently test the working of a program.

The following program demonstrates the use of command line arguments. Basically, the main() method has a parameter that is a string array. The arguments that the user provides at the command line are assigned to this string array with each element represent the corresponding argument in the specified order.

Hence, in the following example, the two arguments that we provide in command line will be stored in args[0], and args[1]. Since the arguments are strings so we need to convert them in integer. Further, the program computes the power of a number using these arguments.

class Main{
    public static void main(String[] args) {
        int n=Integer.parseInt(args[0]);
        int m=Integer.parseInt(args[1]);
        System.out.println("First Number (n): "+n);
        System.out.println("Second Number (m): "+m); 
        int power=1;
        for(int i=1;i<=m;i++)
        System.out.println(n +" raise to the power "+m+" is "+power);


First Number (n): 6
Second Number (m): 8
6 raise to the power 8 is 40320

Further Reading

Java Practice Exercise