简体   繁体   English

“可变长度参数”在 Java 中如何工作?

[英]How does 'variable length arguments' work in Java?

According to my understanding, a method with variable length argument and another method with array as an argument are interchangeable.根据我的理解,具有可变长度参数的方法和另一种以数组为参数的方法是可以互换的。 for eg.例如。

void test(int ... ints){} //method-1

is same as

void test (int [] ints){} //method-2

and we can't use both in the same class (Compile Time Error).并且我们不能在同一个类中同时使用两者(编译时错误)。

When I use method-1, I can call this method by passing some integers or by passing an array of integers since calling this method using some integers will implicitly create an array of those integers.当我使用方法 1 时,我可以通过传递一些整数或传递一个整数数组来调用此方法,因为使用一些整数调用此方法将隐式创建这些整数的数组。

For eg.例如。

test(1,2,3); //ok
test(new int[5]); //ok

But ,但是

When I use method-2, I can't call this method by passing some integers as arguments.当我使用方法 2 时,我无法通过传递一些整数作为参数来调用此方法。

test(new int[5]); //ok
test(1,2,3); //NOT OK

Questions:问题:

1) If java implicitly creates an array of the arguments, why it is not able to call test(1,2,3) in case of method-2. 1) 如果 java 隐式地创建了一个参数数组,为什么在方法 2 的情况下它不能调用 test(1,2,3) 。

2) Why java doesn't allow using method-1 and method-2 together when both show different behavior? 2) 为什么当方法 1 和方法 2 表现出不同的行为时,java 不允许同时使用它们?

One difference I could think of is you can have additional arguments to a metod after an int[] but you cant have a variable argument like that.我能想到的一个区别是,您可以在 int[] 之后为方法添加额外的参数,但不能有这样的变量参数。 Variable argument should always be the last parameter to a method.变量参数应该始终是方法的最后一个参数。 Example例子

void test(int[] a, int a) {} // works fine.
void test(int ... ints, int a){} // compile error.

1) If java implicitly creates an array of the arguments, why it is not able to call test(1,2,3) in case of method-2. 1) 如果 java 隐式地创建了一个参数数组,为什么在方法 2 的情况下它不能调用 test(1,2,3) 。

Because 1,2,3 in test(1,2,3) is not an array, these are three different integers...You have to pass an Array of int to invoke method-2.因为1,2,3test(1,2,3)是不是一个数组,这些三种不同的整数......你要通过一个Arrayint以调用方法-2。

2) Why java doesn't allow using method-1 and method-2 together when both show different behavior? 2) 为什么当方法 1 和方法 2 表现出不同的行为时,java 不允许同时使用它们?

It creates ambiguity when you call method with array as an argument because array as an argument is allowed in case of varargs as well as array as an argument.当您使用数组作为参数调用方法时,它会产生歧义,因为在可变参数和数组作为参数的情况下,允许将数组作为参数。

for example, if both method were allowed, test(new int[5]) will create an ambiguity.例如,如果这两种方法都被允许,则test(new int[5])将产生歧义。

The internal representation in the Java compiler of the function declarations in these two cases is identical.在这两种情况下,函数声明在 Java 编译器中的内部表示是相同的。 That's why the compiler wont let you declare two such functions with the same name.这就是为什么编译器不会让你声明两个同名的函数。

The difference in the bahivour of these two declarations can be seen when you actually call the functions with the actual arguments.当您实际使用实际参数调用函数时,可以看出这两个声明的不同之处。 Since Java is a strongly typed language (with strict type checking) it doesn't allow you to pass three integers to the test (int[] a) method.由于 Java 是一种强类型语言(具有严格的类型检查),它不允许您将三个整数传递给测试 (int[] a) 方法。

However, when you call test(int ... a) with test(1,2,3) the compiler generates intermediate code to make a temporary array of three integers and passes it onto the function.但是,当您使用 test(1,2,3) 调用 test(int ... a) 时,编译器会生成中间代码以创建一个包含三个整数的临时数组并将其传递给函数。

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

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