Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-22
Topic: Command Line Arguments
Command line arguments :
Every Java program has a main method with String [ ] args parameter.
This parameter indicates that the main method receives an array of
strings specified on the command line.
Command line arguments are parameters that are supplied to the
program at the time of involving it for execution.
Any argument provided on command line passed to the array args as its
element.
We can write the program that can receive and use the arguments
provided on the command line.
For Example, Consider the command line.
Java Test Hello 10 World 34.6
Command line arguments
Interpreter Class name args [0] args [1] args [2] args [3]
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Programs on Command Line Arguments:
1) Write a program to print number of command line argument
and their values.
class CmdLine
{
public static void main(String [] args)
{
[Link]("Number of command line arguments =
"+[Link]);
[Link]("Command Line argument values are...");
for(int i=0;i<[Link];i++)
[Link](args[i]);
}
}
Output:
Cmd Line Arguments
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
2) Write a program to read two strings from command line and
then print theirs string concatenation..
class CmdConcat
{
public static void main(String [] args)
{
String s1=args[0];
String s2=args[1];
String s3=s1+s2;
[Link]("String Concatenation is :"+s3);
}
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
3) Write a program to read two numbers from command line and
then print their addition.
class CmdSum
{
public static void main(String [] args)
{
int x = [Link](args[0]);
int y = [Link](args[1]);
int add=x+y;
[Link]("Addition = "+add);
}
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260