Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Command Line Arguments

Command-line arguments let you pass information into a Java program at the moment it starts, without hardcoding values or prompting for input. They arrive as the String[] args parameter of the main method.

Why it matters

Command-line arguments are how scripts, schedulers and deployment tools hand settings to a Java program: an input file, an environment name, a port. Handling them carefully — checking how many arrived and converting them safely — is the difference between a tool that reports "usage: ..." and one that crashes with a stack trace.

How Arguments Are Passed

When you run java ProgramName arg1 arg2 arg3, the JVM places "arg1", "arg2", and "arg3" into the args array in order, as Strings — args[0] is "arg1", args[1] is "arg2", and so on. If no arguments are given, args is an empty array (length 0), not null.

Converting Arguments to Other Types

Because every argument arrives as a String, you must explicitly convert it if you need a number or other type — for example, Integer.parseInt(args[0]) for an int, or Double.parseDouble(args[1]) for a double. Always validate the array length before accessing an index, since accessing a missing index throws an ArrayIndexOutOfBoundsException.

Example

Reading and converting command-line arguments safely

Java
public class ArgsDemo {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: java ArgsDemo <name> <age>");
            return;
        }
        String name = args[0];
        int age = Integer.parseInt(args[1]);
        System.out.println(name + " is " + age + " years old.");
    }
}

// Run with: java ArgsDemo Asha 28
Output
Asha is 28 years old.

Common Mistakes

  • Forgetting that args elements are always Strings and trying to use them directly in arithmetic without parsing.
  • Accessing args[0] without first checking args.length, causing an ArrayIndexOutOfBoundsException when no arguments are supplied.
  • Confusing command-line arguments (passed at program start) with user input read during execution via Scanner — they are different mechanisms.

Exercise

Write a program that adds up all the numbers passed on the command line. With no arguments it should print a usage message instead of failing.

Running java SumArgs 4 5 6 should print Sum: 15. Running java SumArgs should print Usage: java SumArgs <number> ...

Java · starter code
public class SumArgs {
    public static void main(String[] args) {
        // TODO: if there are no arguments, print the usage message and return

        int sum = 0;
        // TODO: convert each argument to an int and add it to sum

        System.out.println("Sum: " + sum);
    }
}
Show hints
  • args.length is 0 when no arguments are given; args is never null.
  • Every argument is a String, so use Integer.parseInt(arg) inside a for-each loop.
Show solution
Java · solution
public class SumArgs {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage: java SumArgs <number> ...");
            return;
        }

        int sum = 0;
        for (String arg : args) {
            sum += Integer.parseInt(arg);
        }

        System.out.println("Sum: " + sum);
    }
}

Quick quiz

1. You run java Demo red green blue. What is args.length?

  • A. 4
  • B. 0
  • C. 2
  • D. 3
Show answer

D. The class name is not part of args, so the array holds the three words that follow it.

2. You run java Demo with no arguments and the program reads args[0]. What happens?

  • A. It throws ArrayIndexOutOfBoundsException
  • B. It reads an empty String
  • C. It reads null
  • D. It does not compile
Show answer

A. args is an empty array, so index 0 does not exist and the access throws ArrayIndexOutOfBoundsException.

3. You run java Demo 42. What is the type of args[0]?

  • A. int
  • B. String
  • C. Integer
  • D. char
Show answer

B. Every command-line argument arrives as a String. Convert it with Integer.parseInt if you need a number.

Interview questions

Can args be null if no arguments are passed?

Show answer

No. When a program is started with no arguments, the JVM passes an empty array of length 0, not null. So args.length is always safe to read, but any index access must be guarded by a length check.

Can main be declared as main(String... args)?

Show answer

Yes. A varargs parameter is compiled as an array, so public static void main(String... args) is a valid entry point. The parameter name is also free to choose; only the type and the modifiers matter to the JVM.

Key Points to Remember

  • Command-line arguments are received as String[] args in the main method.
  • args is never null; with no arguments supplied it is simply an empty array.
  • Always parse and validate arguments before use, since they arrive as raw text.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering