Command Line Arguments

javac Test.java
java Test A B C

When executing a Java program using the command line as shown above the values A, B and C are called "command line arguments". A command line argument is a argument which are passed in from command prompt. When these arguments are used, the JVM will create an array String[] args = {"A","B","C"} containing these arguments, and then calls the main method with the array as an argument to the main method.

import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        System.out.println(args[0]); // A
        System.out.println(args[1]); // B
        System.out.println(args[2]); // C
        System.out.println(args.length); // 3
    }
}

The main objective of command line arguments is to customise the functionality of the main method.

The command line arguments in main methods are always a string because String is the most used object in Java. Also methods already exist to convert any string to other data types like int, boolean etc.

import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        for(int i=0; i <= args.length; i++){
            System.out.println(args[i]);
        }
    }
}
javac Test.java
java Test A B C
A
B
C
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at Test.main(Test.java:8)
javac Test.java
java Test A B
A
B
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
	at Test.main(Test.java:8)
javac Test.java
java Test A B
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at Test.main(Test.java:8)

In the example above, a runtime exception is thrown when iterating through the command line arguments because of using "<=" instead of just "<". So special care needs to be taken when iterating through array elements.

import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        String[] argh = {"x", "y", "z"};
        args = argh;
        for(String s: args){
            System.out.println(s);
        }
    }
}
javac Test.java
java Test A B C
x
y
z
javac Test.java
java Test A B
x
y
z
javac Test.java
java Test
x
y
z
Image illustrating what happens when an array is re-assigned
Image illustrating what happens when an array is re-assigned

The example above demonstrates another gotcha when working with arrays, you must take care not to re-assign arrays to different arrays unintentionally, otherwise the results may not be as expected.

import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        String[] argh = {"x", "y", "z"};
        args = argh;
        for(String s: args){
            System.out.println(s);
        }
    }
}
javac Test.java
java Test 10 20
1020
import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        System.out.println(args[0] + args[1]); // 1020: concatenation not addition, as these are String objects
    }
}

Another thing to be cautious of is that, since command line arguments are always a String object, be careful not to do arithmetic operations on String objects as shown above. In the example above using the "+" operator on String objects results in concatenation, not addition.

import java.lang.*;
import java.util.*;
import java.io.*;

class Test {
    public static void main(String... args) {
        System.out.println(args[0]);
    }
}
javac Test.java
java Test note book
note
java Test "note book"
note book

The example above demonstrates how to correctly parse a command line argument with a space in it. Typically, a space is considered a separator between command line arguments. Enclosing the command line argument in double quotes will allow it to be seen as a single command line argument, instead of two separate arguments.