Java Command Line Arguments - String[] args Explained

⏱️ 7 min read • Beginner Level • Lesson 20

Lesson 20 of 124 of Java Tutorial

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.


What are Command Line Arguments in Java?

Command line arguments are values given after the class name when running a Java program from terminal or command prompt.

Simple Meaning: Instead of asking the user to type input during program execution, you can pass input values while starting the program.
Command Format
java ClassName argument1 argument2 argument3
How Command Line Arguments Work:

User Input ➜ Command Prompt ➜ String[] args ➜ Java Program

Real-World Example: Command line arguments are commonly used to pass configuration values, file names, usernames, passwords, environment settings, and startup options when launching Java applications.

String[] args in main Method

In Java, the main() method contains a one-dimensional array of String type. This array receives command line arguments.

MainMethodArgs.java
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.
  • All command line arguments are received as String values.

Example: Single Command Line Argument

In this example, we receive one command line argument and print it.

CommandLineDemo.java
Copy Download
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
Important: If you run this program without any argument, args[0] will cause an error because no first argument exists.

Example: Multiple Command Line Arguments

You can pass any number of command line arguments. Arguments are separated by spaces. Java stores them in the args array.

MultipleArgumentsDemo.java
Copy Download
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

Passing Arguments with Spaces

By default, spaces separate command line arguments. If you want to pass a value containing spaces, use double quotes.

ArgumentWithSpaces.java
public class ArgumentWithSpaces {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

Run:

java ArgumentWithSpaces "Java Programming"

Output:

Java Programming

Using Numeric Command Line Arguments

Command line arguments are received as strings. To use them as numbers, you must convert them using methods like Integer.parseInt().

AddNumbers.java
Copy Download
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

Checking Number of Arguments

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.

ArgumentCheck.java
Copy Download
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 vs Command Line Arguments

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.

Common Mistakes with Command Line Arguments

  • Accessing args[0] without checking args.length.
  • Forgetting that all command line arguments are received as String.
  • Not converting numeric arguments before arithmetic operations.
  • Forgetting to use quotes for arguments containing spaces.
  • Running the program without passing required arguments.

Command Line Arguments at a Glance

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()
Summary:
  • Command line arguments are passed when running a Java program.
  • They are received in the String[] args parameter of the main() method.
  • args[0] stores the first argument.
  • args.length gives the number of arguments.
  • All command line arguments are received as strings.
  • Numeric arguments must be converted before calculations.

Frequently Asked Questions

Command line arguments in Java are values passed to a program at the time of execution. They are received in the main method through the String[] args array.

Command line arguments are stored in the String[] args parameter of the main method.

args[0] represents the first command line argument passed to the Java program.

args.length returns the number of command line arguments passed to the Java program.

Yes, command line arguments are always received as String values in Java. Numeric values must be converted before arithmetic operations.

Multiple command line arguments are passed after the class name separated by spaces, such as java ProgramName Java Python C.

To pass an argument with spaces, enclose it in double quotes, such as java ProgramName "Java Programming".

You can convert command line arguments to numbers using methods such as Integer.parseInt(), Double.parseDouble(), or Float.parseFloat().

If args[0] is used without passing any argument, Java throws an ArrayIndexOutOfBoundsException at runtime.

Scanner input is entered while the program is running, while command line arguments are passed when starting the program.

Next step: Learn Java If Statement

🚀 Continue to Java If Statement →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Command-line-arguments | Language: Java

Q1. What are command line arguments in Java?
Q2. How are command line arguments separated by default?
Q3. Which method can convert a command line argument to an int?
Q4. What will be the output if this program is run as: java Add 10 20?
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);
    }
}
Q5. Where are command line arguments received in Java?
Q6. Why should args.length be checked before accessing args[0]?
Q7. Which is better for interactive user input during program execution?
Q8. Which command correctly passes two arguments to a Java program named Test?
Q9. What happens if args[0] is accessed without passing any argument?
Q10. Which statement is true about command line arguments?
Q11. How can you pass Java Programming as a single command line argument?
Q12. What is the data type of command line arguments in Java?
Q13. What does args[0] represent?
Q14. What does args.length return?
Q15. What will be the output of this code if run as: java Demo Java?
public class Demo {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

🎉 Great job! Continue learning Java step by step.