Java Command Line Arguments: Passing Parameters via String[] args

Command-line arguments allow users to customize program execution by passing input parameters directly from the terminal or console when launching the JVM.

How Command-Line Arguments Work

Every value passed after the class name is captured as an element inside the String[] args array:

# Terminal invocation:
java Calculator 15 25 add

Practical Example: Building a Command-Line Calculator

public class Calculator {
    public static void main(String[] args) {
        // Validate argument length
        if (args.length < 3) {
            System.out.println("Usage: java Calculator <num1> <num2> <add|sub|mul|div>");
            return;
        }

        // Convert String arguments to numerical primitives
        double num1 = Double.parseDouble(args[0]);
        double num2 = Double.parseDouble(args[1]);
        String operation = args[2].toLowerCase();

        double result = switch (operation) {
            case "add" -> num1 + num2;
            case "sub" -> num1 - num2;
            case "mul" -> num1 * num2;
            case "div" -> {
                if (num2 == 0) throw new ArithmeticException("Cannot divide by zero!");
                yield num1 / num2;
            }
            default -> throw new IllegalArgumentException("Unknown operator: " + operation);
        };

        System.out.println("Result: " + result);
    }
}

University Exam Corner (BCA / B.Sc CS)

Exam Question: "Can we rename String[] args to String[] params or use varargs String... args?"

Answer: Yes! The parameter name args is purely conventional; you can rename it to String[] myArgs without issue. Furthermore, since Java 5, writing public static void main(String... args) (using varargs syntax) is 100% valid and recognized by the JVM launcher!

Next Step

Now let us explore the immutable Java String class, memory allocation, and the String Constant Pool. Proceed to: Java String Class & SCP Pool →

Frequently Asked Questions (FAQ)

Q1. What is String[] args in the Java main method?
String[] args is an array of String objects that captures all command-line arguments passed to the program when launched from the terminal (e.g. 'java MyApp arg1 arg2').
Q2. What is args.length if no arguments are passed from the command line?
If no arguments are provided, the JVM passes an empty, non-null array with a length of 0 (args.length == 0). It is never null.
Q3. How can I pass an argument that contains spaces?
Enclose the multi-word argument in double quotes in your terminal (e.g. java MyApp "New Delhi" 110001).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Arrays Basics →

Understanding array structures.

Wrapper Classes →

Parsing numbers with Integer.parseInt().

Main Method Signature →

Public static void main breakdown.

Java String Class →

String manipulation.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!