Samrat Chowdhury
Samrat Chowdhury

Reputation: 11

Java Command Line Argument

The following is ABC.java:

public class ABC{
    public static void main(String[] sam){}
}

Now if i run it with:

1) java ABC

is the sam array will be empty or null?

Upvotes: 0

Views: 67

Answers (3)

TSKSwamy
TSKSwamy

Reputation: 225

It will be empty.

public class ABC {

    public static void main(String[] sam) {
        if (sam == null) {
            System.out.println("sam is null");
        }
        if (sam.length == 0) {
            System.out.println("sam is empty");
        }
    }

}

Output: sam is empty

Upvotes: 1

amahfouz
amahfouz

Reputation: 2398

Never null. Empty array designates no parameters. In general, it is good to preserve the semantics that "the number of elements in the array is the number of arguments".

Upvotes: 1

Thilo
Thilo

Reputation: 262464

It will be an empty array. So you don't need to check for null (but you need to check for the length before trying to pull out parameters).

Also note that the method signature is fixed, even if you don't need parameters, you need to have that String[].

Upvotes: 3

Related Questions