Introduction
Command Line Arguments рд╡реЗ values рд╣реЛрддреА рд╣реИрдВ рдЬреЛ Java program рдХреЛ run рдХрд░рддреЗ рд╕рдордп command prompt рдпрд╛ terminal рд╕реЗ рджреА рдЬрд╛рддреА рд╣реИрдВред Java рдореЗрдВ command line arguments main() method рдХреЗ рдЕрдВрджрд░ рд▓рд┐рдЦреЗ рдЧрдП String[] args рдореЗрдВ store рд╣реЛрддреЗ рд╣реИрдВред
Command line arguments рдХреА help рд╕реЗ user program рдХреЛ run рдХрд░рддреЗ рд╕рдордп input рджреЗ рд╕рдХрддрд╛ рд╣реИред рдЗрд╕рдХреЗ рд▓рд┐рдП program рдореЗрдВ input рд▓реЗрдиреЗ рдХреЗ рд▓рд┐рдП Scanner class рдХреА рдЬрд░реВрд░рдд рдирд╣реАрдВ рдкрдбрд╝рддреАред
Simple Definition
Command line arguments are the values passed to a Java program at the time of execution through command prompt or terminal.
Hindi Meaning
Command line arguments рд╡реЗ values рд╣реЛрддреА рд╣реИрдВ рдЬреЛ Java program рдХреЛ run рдХрд░рддреЗ рд╕рдордп command prompt рдпрд╛ terminal рд╕реЗ рджреА рдЬрд╛рддреА рд╣реИрдВред
Syntax of main Method
public static void main(String[] args)
рдЗрд╕ syntax рдореЗрдВ String[] args command line arguments рдХреЛ store рдХрд░рддрд╛ рд╣реИред рдпрд╣рд╛рдБ args рдПрдХ string array рд╣реИред
Meaning of String[] args
String[] args рдХрд╛ рдорддрд▓рдм рд╣реИ string type рдХрд╛ arrayред рдЬрдм user command line рд╕реЗ values pass рдХрд░рддрд╛ рд╣реИ, рддреЛ рд╡реЗ values args array рдореЗрдВ store рд╣реЛ рдЬрд╛рддреА рд╣реИрдВред
Example
java Demo Rahul BCA
рдЗрд╕ command рдореЗрдВ:
args[0] = Rahul
args[1] = BCA
How to Pass Command Line Arguments
рд╕рдмрд╕реЗ рдкрд╣рд▓реЗ Java program рдХреЛ compile рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЙрд╕рдХреЗ рдмрд╛рдж program рдХреЛ run рдХрд░рддреЗ рд╕рдордп class name рдХреЗ рдмрд╛рдж values рд▓рд┐рдЦреА рдЬрд╛рддреА рд╣реИрдВред
Compile Command
javac Demo.java
Run Command with Arguments
java Demo Rahul BCA
рдпрд╣рд╛рдБ Rahul рдФрд░ BCA command line arguments рд╣реИрдВред
Simple Command Line Argument Program
class Demo {
public static void main(String[] args) {
System.out.println("First Argument: " + args[0]);
System.out.println("Second Argument: " + args[1]);
}
}
Run Command
java Demo Rahul BCA
Output
First Argument: Rahul
Second Argument: BCA
Explanation
рдЗрд╕ program рдореЗрдВ command line рд╕реЗ рджреЛ values pass рдХреА рдЧрдИ рд╣реИрдВред рдкрд╣рд▓реА value args[0] рдореЗрдВ рдФрд░ рджреВрд╕рд░реА value args[1] рдореЗрдВ store рд╣реЛрддреА рд╣реИред
Important Point
Command line arguments рд╣рдореЗрд╢рд╛ string form рдореЗрдВ store рд╣реЛрддреЗ рд╣реИрдВред рдЕрдЧрд░ user number pass рдХрд░рддрд╛ рд╣реИ, рддрдм рднреА рд╡рд╣ string рдХреЗ рд░реВрдк рдореЗрдВ store рд╣реЛрддрд╛ рд╣реИред Calculation рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП string рдХреЛ number рдореЗрдВ convert рдХрд░рдирд╛ рдкрдбрд╝рддрд╛ рд╣реИред
Example: Print Name Using Command Line Argument
class NameDemo {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}
Run Command
java NameDemo Amit
Output
Hello Amit
Example: Print Student Details
class StudentInfo {
public static void main(String[] args) {
System.out.println("Name: " + args[0]);
System.out.println("Course: " + args[1]);
System.out.println("Semester: " + args[2]);
}
}
Run Command
java StudentInfo Rahul BCA First
Output
Name: Rahul
Course: BCA
Semester: First
Command Line Arguments with Numbers
Command line рд╕реЗ pass рдХреА рдЧрдИ values string рд╣реЛрддреА рд╣реИрдВред рдЗрд╕рд▓рд┐рдП numbers рдкрд░ calculation рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЙрдиреНрд╣реЗрдВ integer рдпрд╛ double рдореЗрдВ convert рдХрд░рдирд╛ рдкрдбрд╝рддрд╛ рд╣реИред
Java рдореЗрдВ string рдХреЛ integer рдореЗрдВ convert рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Integer.parseInt() method use рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Example: Addition Using Command Line Arguments
class AddNumbers {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum = " + sum);
}
}
Run Command
java AddNumbers 10 20
Output
Sum = 30
Explanation
рдЗрд╕ program рдореЗрдВ args[0] рдФрд░ args[1] string form рдореЗрдВ values store рдХрд░рддреЗ рд╣реИрдВред Integer.parseInt() method рдЗрди strings рдХреЛ integer рдореЗрдВ convert рдХрд░рддрд╛ рд╣реИред рдлрд┐рд░ рджреЛрдиреЛрдВ numbers рдХрд╛ addition рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Example: Multiplication Using Command Line Arguments
class MultiplyNumbers {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("Multiplication = " + (a * b));
}
}
Run Command
java MultiplyNumbers 5 6
Output
Multiplication = 30
Example: Double Values Using Command Line Arguments
String рдХреЛ double рдореЗрдВ convert рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Double.parseDouble() method use рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
class AverageDemo {
public static void main(String[] args) {
double marks1 = Double.parseDouble(args[0]);
double marks2 = Double.parseDouble(args[1]);
double average = (marks1 + marks2) / 2;
System.out.println("Average = " + average);
}
}
Run Command
java AverageDemo 80.5 90.5
Output
Average = 85.5
args.length
args.length command line arguments рдХреА total рд╕рдВрдЦреНрдпрд╛ рдмрддрд╛рддрд╛ рд╣реИред
Example
class CountArguments {
public static void main(String[] args) {
System.out.println("Total Arguments: " + args.length);
}
}
Run Command
java CountArguments Java BCA Student
Output
Total Arguments: 3
Display All Command Line Arguments
class DisplayArguments {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + i + ": " + args[i]);
}
}
}
Run Command
java DisplayArguments Java Programming BCA
Output
Argument 0: Java
Argument 1: Programming
Argument 2: BCA
Explanation
рдЗрд╕ program рдореЗрдВ for loop рдХреА help рд╕реЗ command line рд╕реЗ pass рдХрд┐рдП рдЧрдП рд╕рднреА arguments print рдХрд┐рдП рдЧрдП рд╣реИрдВред
Command Line Arguments рдФрд░ Scanner рдореЗрдВ Difference
| Command Line Arguments | Scanner |
|---|---|
| Input program run рдХрд░рддреЗ рд╕рдордп рджрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред | Input program run рд╣реЛрдиреЗ рдХреЗ рдмрд╛рдж рджрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред |
Values String[] args рдореЗрдВ store рд╣реЛрддреА рд╣реИрдВред | Values Scanner object рд╕реЗ read рд╣реЛрддреА рд╣реИрдВред |
| рд╕рднреА values string form рдореЗрдВ рд╣реЛрддреА рд╣реИрдВред | Different data types directly read рдХрд┐рдП рдЬрд╛ рд╕рдХрддреЗ рд╣реИрдВред |
| Small programs рдХреЗ рд▓рд┐рдП useful рд╣реИред | Interactive programs рдХреЗ рд▓рд┐рдП useful рд╣реИред |
Advantages of Command Line Arguments
- Program run рдХрд░рддреЗ рд╕рдордп input рджрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
- Simple рдФрд░ fast method рд╣реИред
- Scanner class рдХреА рдЬрд░реВрд░рдд рдирд╣реАрдВ рдкрдбрд╝рддреАред
- Testing рдХреЗ рд▓рд┐рдП useful рд╣реИред
- Multiple values рдПрдХ рд╕рд╛рде pass рдХреА рдЬрд╛ рд╕рдХрддреА рд╣реИрдВред
Limitations of Command Line Arguments
- рд╕рднреА values string form рдореЗрдВ store рд╣реЛрддреА рд╣реИрдВред
- Numeric calculation рдХреЗ рд▓рд┐рдП type conversion рдЬрд░реВрд░реА рд╣реИред
- User-friendly input method рдирд╣реАрдВ рд╣реИред
- Arguments рди рджреЗрдиреЗ рдкрд░ error рдЖ рд╕рдХрддрд╛ рд╣реИред
- Complex input рдХреЗ рд▓рд┐рдП Scanner рдЬреНрдпрд╛рджрд╛ useful рд╣реЛрддрд╛ рд╣реИред
Common Error: ArrayIndexOutOfBoundsException
рдЕрдЧрд░ program рдореЗрдВ args[0] use рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ рд▓реЗрдХрд┐рди run command рдореЗрдВ рдХреЛрдИ argument pass рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЧрдпрд╛, рддреЛ error рдЖрдПрдЧрд╛ред
Example
class ErrorDemo {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
Wrong Run Command
java ErrorDemo
Error
ArrayIndexOutOfBoundsException
Correct Run Command
java ErrorDemo Hello
Output
Hello
Safe Program Using args.length
class SafeArgument {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Argument: " + args[0]);
} else {
System.out.println("No argument provided");
}
}
}
Run Command
java SafeArgument
Output
No argument provided
Run Command with Argument
java SafeArgument Java
Output
Argument: Java
Exam-Oriented Short Answer
Q. What are command line arguments in Java?
Command line arguments рд╡реЗ values рд╣реЛрддреА рд╣реИрдВ рдЬреЛ Java program рдХреЛ run рдХрд░рддреЗ рд╕рдордп command prompt рдпрд╛ terminal рд╕реЗ pass рдХреА рдЬрд╛рддреА рд╣реИрдВред рдпреЗ values main() method рдХреЗ String[] args array рдореЗрдВ store рд╣реЛрддреА рд╣реИрдВред Command line arguments рд╣рдореЗрд╢рд╛ string form рдореЗрдВ рд╣реЛрддреЗ рд╣реИрдВред рдЕрдЧрд░ numeric calculation рдХрд░рдиреА рд╣реЛ, рддреЛ рдЗрдиреНрд╣реЗрдВ Integer.parseInt() рдпрд╛ Double.parseDouble() methods рдХреА help рд╕реЗ convert рдХрд░рдирд╛ рдкрдбрд╝рддрд╛ рд╣реИред
Important Points to Remember
- Command line arguments program run рдХрд░рддреЗ рд╕рдордп pass рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВред
- рдпреЗ arguments
String[] argsрдореЗрдВ store рд╣реЛрддреЗ рд╣реИрдВред argsрдПрдХ string array рд╣реИред- First argument
args[0]рдореЗрдВ store рд╣реЛрддрд╛ рд╣реИред - рд╕рднреА command line arguments string form рдореЗрдВ рд╣реЛрддреЗ рд╣реИрдВред
- Numeric values рдХреЗ рд▓рд┐рдП type conversion рдЬрд░реВрд░реА рд╣реИред
args.lengthtotal arguments count рдмрддрд╛рддрд╛ рд╣реИред- Arguments рди рджреЗрдиреЗ рдкрд░
ArrayIndexOutOfBoundsExceptionрдЖ рд╕рдХрддрд╛ рд╣реИред
Practice Questions
Short Answer Questions
- Command line arguments рдХреНрдпрд╛ рд╣реЛрддреЗ рд╣реИрдВ?
String[] argsрдХрд╛ рдХреНрдпрд╛ use рд╣реИ?args.lengthрдХреНрдпрд╛ рдмрддрд╛рддрд╛ рд╣реИ?- Command line arguments рдФрд░ Scanner рдореЗрдВ рдЕрдВрддрд░ рд▓рд┐рдЦрд┐рдПред
- Command line argument рдХреЛ integer рдореЗрдВ рдХреИрд╕реЗ convert рдХрд░рддреЗ рд╣реИрдВ?
ArrayIndexOutOfBoundsExceptionрдХрдм рдЖрддреА рд╣реИ?
Long Answer Question
Command line arguments рдХреЛ Java program рдХреЗ example рдХреЗ рд╕рд╛рде explain рдХреАрдЬрд┐рдПред
Homework Program
Command line arguments рдХреА help рд╕реЗ рджреЛ numbers input рд▓реЗрдХрд░ рдЙрдирдХрд╛ sum, subtraction рдФрд░ multiplication print рдХрд░рдиреЗ рдХрд╛ Java program рд▓рд┐рдЦрд┐рдПред
Hint
class CalculatorArgs {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("Sum = " + (a + b));
System.out.println("Subtraction = " + (a - b));
System.out.println("Multiplication = " + (a * b));
}
}
Run Command
java CalculatorArgs 20 10
Output
Sum = 30
Subtraction = 10
Multiplication = 200