简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM