⏱️ 7 min read • Beginner Level • Lesson 20
Command line arguments in Java are values passed to a program at the time of execution.
These values are received inside the main() method using the String[] args array.
Before learning command line arguments, you should understand Java Input Output, Java Program Structure, and Java Arrays.
Command line arguments are values given after the class name when running a Java program from terminal or command prompt.
java ClassName argument1 argument2 argument3
User Input ➜ Command Prompt ➜ String[] args ➜ Java Program
In Java, the main() method contains a one-dimensional array of String type.
This array receives command line arguments.
public static void main(String[] args) {
// args stores command line arguments
}
args[0] stores the first argument.args[1] stores the second argument.args.length gives the number of arguments.String values.In this example, we receive one command line argument and print it.
public class CommandLineDemo {
public static void main(String[] args) {
String firstArgument = args[0];
System.out.println("Argument is: " + firstArgument);
}
}
Compile and Run:
javac CommandLineDemo.java java CommandLineDemo JavaProwess
Output:
Argument is: JavaProwess
args[0] will cause an error because no first argument exists.
You can pass any number of command line arguments. Arguments are separated by spaces.
Java stores them in the args array.
public class MultipleArgumentsDemo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
}
}
}
Compile and Run:
javac MultipleArgumentsDemo.java java MultipleArgumentsDemo C C++ and "Java Prowess"
Output:
C C++ and Java Prowess
By default, spaces separate command line arguments. If you want to pass a value containing spaces, use double quotes.
public class ArgumentWithSpaces {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
Run:
java ArgumentWithSpaces "Java Programming"
Output:
Java Programming
Command line arguments are received as strings. To use them as numbers,
you must convert them using methods like Integer.parseInt().
public class AddNumbers {
public static void main(String[] args) {
int number1 = Integer.parseInt(args[0]);
int number2 = Integer.parseInt(args[1]);
int sum = number1 + number2;
System.out.println("Sum: " + sum);
}
}
Compile and Run:
javac AddNumbers.java java AddNumbers 10 20
Output:
Sum: 30
It is a good practice to check args.length before accessing command line arguments.
This helps avoid errors when the user forgets to pass arguments.
public class ArgumentCheck {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please pass at least one argument.");
return;
}
System.out.println("First Argument: " + args[0]);
}
}
Run without argument:
java ArgumentCheck
Output:
Please pass at least one argument.
| Scanner Input | Command Line Arguments |
|---|---|
| User enters input while the program is running. | User passes input while starting the program. |
Uses classes like Scanner. |
Uses String[] args. |
| Good for interactive programs. | Good for configuration, quick testing, and automation. |
| Can read input step by step. | All values are available at program start. |
args[0] without checking args.length.String.| Concept | Description |
|---|---|
| args[0] | First command line argument |
| args[1] | Second command line argument |
| args.length | Number of arguments |
| Data Type | String |
| Numeric Conversion | Integer.parseInt() |
String[] args parameter of the main() method.args[0] stores the first argument.args.length gives the number of arguments.🧠 Test your understanding with a quick quiz
Topic: Command-line-arguments | Language: Java
public class Add {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println(a + b);
}
}public class Demo {
public static void main(String[] args) {
System.out.println(args[0]);
}
}🎉 Great job! Continue learning Java step by step.