Command Line Arguments
Command Line Arguments
The arguments passed from the console can be received in the java program and
it can be used as an input.
It provides a convenient way to check the behavior of the program for the
different values.
You can pass N (1,2,3 and so on) numbers of arguments from the command
prompt.
The command line arguments can be accessed easily, because they are stored as
Strings in String array passed to the args in the main method.
The first command line argument is stored in args[0], the second argument is
stored in args[1], the third argument is stored in args[2], and so on.
Program:
class Test
System.out.println(“Number of arguments=”+args.length);
for(int i=0;i<args.length;i++)</args.length;i++)
System.out.println(args[i]);
}}
Output:
Program 2:
class Test2
int x,y;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
System.out.println("Sum="+(x+y));
Output: