Java Command-Line Arguments: String[] args, Examples and Usage

0

Until now, we have seen programs that receive input while they are running through the console. Java also provides another way to provide input: command-line arguments. These values are supplied when the program is started rather than being entered interactively after the program begins.

What Are Command-Line Arguments?

Command-line arguments are values passed to a Java program when the program is launched. Java receives these values through the String[] args parameter of the main() method.

public class Main {
    public static void main(String[] args) {

        System.out.println(args[0]);
    }
}

Here, args is an array of Strings. Values provided when the program starts are stored inside this array.

The important idea is simple: command-line arguments are supplied when starting the program, while Scanner input is normally supplied after the program starts running.

Understanding String[] args

A normal Java main method is written like this:

public static void main(String[] args) {
}

The args variable represents an array that can contain command-line arguments. Because it is a String array, every command-line argument initially arrives as a String.

Part Meaning
public Allows the JVM to access the main method.
static Allows the method to be called without creating an object.
void The method does not return a value.
main The standard entry point of a Java application.
String[] Defines an array of String values.
args Reference to the command-line argument array.

Passing a Single Argument

Suppose we start the program and provide the value Bibhu as an argument. The program can access it using index 0.

public class Main {
    public static void main(String[] args) {

        System.out.println("Hello " + args[0]);
    }
}

If the program receives Bibhu, the output will be:

Hello Bibhu

The first argument is stored at index 0 because Java arrays use zero-based indexing.

Passing Multiple Arguments

A program can receive multiple command-line arguments.

public class Main {
    public static void main(String[] args) {

        System.out.println("Name: " + args[0]);
        System.out.println("City: " + args[1]);
        System.out.println("Course: " + args[2]);
    }
}

If the arguments are provided in this order:

Bibhu Berhampur Java

Then the values are stored like this:

Index Value
args[0] Bibhu
args[1] Berhampur
args[2] Java

Command-Line Arguments Are Strings

One important point is that command-line arguments are received as Strings, even when the value looks like a number.

public class Main {
    public static void main(String[] args) {

        System.out.println(args[0]);
    }
}

If the argument is 25, Java initially treats it as the String "25", not as the integer 25.

Converting Arguments to Numbers

If you want to perform arithmetic using a command-line argument, you must convert the String into the required numeric type.

public class Main {
    public static void main(String[] args) {

        int age = Integer.parseInt(args[0]);

        System.out.println("Age: " + age);
    }
}

The Integer.parseInt() method converts the String argument into an integer.

The same idea can be used for decimal values.

public class Main {
    public static void main(String[] args) {

        double price = Double.parseDouble(args[0]);

        System.out.println("Price: " + price);
    }
}

Command-Line Arguments for Calculations

Once arguments are converted into numbers, they can participate in calculations.

public class Main {
    public static void main(String[] args) {

        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);

        int total = a + b;

        System.out.println("Total: " + total);
    }
}

If the program receives 10 and 20, the output is:

Total: 30

The important workflow is: receive the arguments as Strings, convert them when necessary, and then use the converted values in the program.

Checking the Number of Arguments

A program should not blindly access an argument that may not exist. The length property can be used to check how many arguments were supplied.

public class Main {
    public static void main(String[] args) {

        System.out.println("Number of arguments: " + args.length);
    }
}

If three arguments are provided, args.length will contain 3.

This is useful because it helps prevent an ArrayIndexOutOfBoundsException when the program expects an argument that was not supplied.

Common Beginner Mistake

A frequent mistake is accessing args[0] without providing any argument.

public class Main {
    public static void main(String[] args) {

        System.out.println(args[0]);
    }
}

If no argument is supplied, there is no element at index 0. The program can therefore fail at runtime.

A safer beginner-level approach is to check the array length first.

public class Main {
    public static void main(String[] args) {

        if (args.length > 0) {
            System.out.println(args[0]);
        } else {
            System.out.println("No argument provided");
        }
    }
}

Command-Line Arguments vs Scanner

Both approaches allow a program to receive external data, but the timing and usage are different.

Feature Command-Line Arguments Scanner Input
When input is supplied When starting the program Usually while the program is running
Input storage String[] args Scanner methods and variables
Initial data type String Can directly read several data types
Typical use Startup options and parameters Interactive user input

Real-World Connection

Command-line arguments are particularly useful when a program needs configuration or startup information. For example, a command-line application might receive a file name, an environment name, or an operation to perform when the application starts.

This approach is common in development tools and command-line utilities because the user can provide instructions directly when launching the program.

Interview Insight

A common interview question is: "What is String[] args in the main method?" A concise answer is that String[] args is a String array used by the Java runtime to provide command-line arguments to the application's main method.

Another important interview point is that command-line arguments are initially Strings. If numeric calculations are required, the values must be explicitly converted using methods such as Integer.parseInt() or Double.parseDouble().

Quick Revision

Concept Remember
Command-Line Argument A value supplied when starting a Java program.
String[] args Array containing the supplied arguments.
args[0] First command-line argument.
args.length Number of supplied arguments.
Integer.parseInt() Converts a String argument into an int.
Double.parseDouble() Converts a String argument into a double.
Important caution Check args.length before accessing an index that may not exist.

Command-line arguments complete an important part of Java's basic input and output story. You have now seen how programs can receive interactive input, read data through Scanner and BufferedReader, display output, format that output, and accept values at startup through command-line arguments.

Post a Comment

0Comments
Post a Comment (0)