简体   繁体   English

Java命令行参数

[英]Java Command Line Argument

The following is ABC.java: 以下是ABC.java:

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

Now if i run it with: 现在,如果我用以下命令运行它:

1) java ABC 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). 因此,您无需检查是否为null(但在尝试提取参数之前需要检查其长度)。

Also note that the method signature is fixed, even if you don't need parameters, you need to have that String[] . 还要注意,方法签名是固定的,即使不需要参数,也需要具有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 输出:sam为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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