Java Command Line Arguments
• Command Line Arguments:
– If any input value is passed through the
command prompt at the time of
running of the program is known
as command line argument
– are parameters that are supplied to the
program at the time of invoking it for
execution
– Accepts any number of arguments
– The arguments passed from the console
can be received in the java program and
it can be used as an input
15:44
Java Command Line Arguments
• Command Line Arguments:
– If any input value is passed through the
command prompt at the time of
running of the program is known
as command line argument
– By default every command line
argument will be treated as string value
– Stored in string array of main method
– Stored in string array args[ ]
15:44
Java Command Line Arguments
• Command Line Arguments:
– specifies them after the name of the
class to be run.
– To use command line arguments the
main( ) method is used as follows :
public static void main(String args[ ])
15:44
Java Command Line Arguments
• Example :
– Consider a Test program that requires
four command line arguments then to
the run the program :
javac [Link]
java Test 11 22 33 44
15:44
Java Command Line Arguments
The command line contains four arguments,
that are : 11 22 33 and 44
These values are stored in an array args[ ] as
follows :
11 args[0]
22 args[1]
33 args[2]
15:44
44 args[3]
Java Command Line Arguments
The individual elements of an array args[ ]
are accessed by using an index or subscript
like
args[i]
Where i is subscript number
15:44
Command Line Arguments - Example
class Test
{
public static void main(String args[ ])
{
[Link](args[0]);
[Link](args[1]);
[Link](args[2]);
[Link](args[3]);
}
}
15:44
Command Line Arguments - Example
public class AddTwoNumbers3
{
public static void main(String args[])
{
int num1, num2, sum;
num1 = [Link](args[0]);
num2 = [Link](args[1]);
sum = num1 + num2;
[Link]("Sum = "+sum);
}
}
15:44
Java Command Line Arguments
The command line contains four arguments,
that are : 11 22 33 and 44
These values are stored in an array args[ ] as
follows :
11 args[0]
22 args[1]
33 args[2]
15:44
44 args[3]
Java Command Line Arguments
The individual elements of an array args[ ]
are accessed by using an index or subscript
like
args[i]
Where i is subscript number
15:44