0% found this document useful (0 votes)
23 views4 pages

Java Lec-22 Command Line Arguments.488d7fb

The document discusses command line arguments in Java programs. It explains that main methods can receive command line arguments via a String array parameter. It provides examples of programs that print argument details, concatenate strings from arguments, and add numbers from arguments.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Java Lec-22 Command Line Arguments.488d7fb

The document discusses command line arguments in Java programs. It explains that main methods can receive command line arguments via a String array parameter. It provides examples of programs that print argument details, concatenate strings from arguments, and add numbers from arguments.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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)
{
System.out.println("Number of command line arguments =
"+args.length);

System.out.println("Command Line argument values are...");

for(int i=0;i<args.length;i++)
System.out.println(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;

System.out.println("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 = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int add=x+y;

System.out.println("Addition = "+add);

}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like