简体   繁体   English

Java语言规范的哪一部分描述了省略的varargs的行为?

[英]Which part of the Java Language Specification describes the behaviour of omitted varargs?

I am looking for a relevant portion of the Java Language Specification (JLS) which describes the behaviour when invoking a variable arity (vararg) method. 我正在寻找Java语言规范(JLS)的相关部分,它描述了调用变量arity(vararg)方法时的行为。

Consider the method: 考虑方法:

public static void printVarArgs(String... args) {
    System.out.println(Arrays.toString(args));
}

If I invoke the method like so: 如果我像这样调用方法:

printVarArgs();

The output will look like: [] because the omission of args at the call site has been converted into an empty array in the printVarArgs method. 输出将如下所示: []因为在调用站点中省略了args已在printVarArgs方法中转换为空数组。

I am looking for the point of the JLS which defines this behaviour. 我正在寻找定义此行为的JLS的观点。 The closest I have found is 15.12.4.2 Evaluate Arguments , but it doesn't give this example, and I'm not sure if this case is actually covered by the formal/mathematical description. 我找到的最接近的是15.12.4.2评估参数 ,但它没有给出这个例子,我不确定这种情况是否实际涵盖在正式/数学描述中。

Which part of the JLS describes the automatic creation of an empty array when a vararg is omitted? 当省略vararg时,JLS的哪一部分描述了自动创建空数组?

From JLS 15.12.4.2: 从JLS 15.12.4.2开始:

The final formal parameter of m necessarily has type T[] for some T, and m is necessarily being invoked with k >= 0 actual argument expressions. 对于某些T,m的最终形式参数必然具有类型T [],并且必须使用k> = 0实际参数表达式调用m。

That's from the callee perspective. 这是从被调用者的角度来看的。 I'm not sure where it states from the caller's perspective the behavior you cite, but it's kind of implied. 从调用者的角度来看,我不确定你引用的行为在哪里,但它有点暗示。

The text of that JLS section says: 该JLS部分的文字说:

If the method being invoked is a variable arity method (§8.4.1) m , it necessarily has n > 0 formal parameters. 如果被调用的方法是变量arity方法(§8.4.1) m ,则它必须具有n > 0形式参数。 The final formal parameter of m necessarily has type T[] for some T , and m is necessarily being invoked with k >= 0 actual argument expressions. 的最终形式参数m一定的类型为T[]对于一些T ,并且m是必然被与调用k >= 0的实际参数的表达式。

If m is being invoked with kn actual argument expressions, or, if m is being invoked with k != n actual argument expressions and the type of the kth argument expression is not assignment compatible with T[] , then the argument list (e1, ... , en-1, en, ...ek) is evaluated as if it were written as (e1, ..., en-1, new T[]{en, ..., ek}) . 如果使用kn实际参数表达式调用m,或者,如果使用k != n实际参数表达式调用m并且第k个参数表达式的类型与T[]不匹配,则参数列表(e1, ... , en-1, en, ...ek)被评估为好像它被写为(e1, ..., en-1, new T[]{en, ..., ek})

In the case you are talking about, there are k == n - 1 formal arguments, so en, ..., ek is an empty sequence, and that means the argument is evaluated as if it was (e1, ..., en-1, new T[]{}) . 在你谈论的情况下,有k == n - 1形式参数,所以en, ..., ek是一个空序列,这意味着参数被评估为好像是(e1, ..., en-1, new T[]{})

In other words, the behaviour is specified in the section you were looking at. 换句话说,行为在您正在查看的部分中指定。

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

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