0

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?

0

3 Answers 3

3

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[].

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.