- Sometimes we need to pass arguments with main () function
- We can do so by using command-line arguments
- Command-line argument is the information that is passed with program name while running the program
- All the arguments, passed to the program are stored inside the String array args[] of the main ()
- Each arguments is stored at the specific index of the array args[]
- For example the first argument is stored at args[0], second at args[1], and so on
- We can accesses specific arguments in the program by its index
- The following program accepts two arguments for summation
Class commandLineDemo
{
Public static void main(String args[])
{
int x,y;
System.out.println(“No. of Arguments are:” + args.length);
if( args.length==2)
{
x=integer.parseInt(args[0]);
y=integer.parseInt(args[1]);
System.out.println(“Sum is:” + (x+y));
}
else
System.out.println(“No. of Arguments are Invalid);
}
}