Java varags方法参数列表与数组 [英] Java varags method param list vs. array

查看:270
本文介绍了Java varags方法参数列表与数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Varargs:

public static void foo(String... string_array) { ... }

单阵列参数:

public static void bar(String[] string_array) { ... }

Java 1.6似乎接受/拒绝以下内容:

Java 1.6 seems to accept/reject the following:

String[] arr = {"abc", "def", "ghi"};
foo(arr);  // accept
bar(arr);  // accept
foo("abc", "def", "ghi");  // accept
bar("abc", "def", "ghi");  // reject

假设上述内容为真/正确,为什么不总是使用varargs而不是单个数组参数?似乎可以免费添加一些来电灵活性。

Assuming the above is true/correct, why not always use varargs instead of single array param? Seems to add a touch of caller flexiblity for free.

专家是否可以共享内部JVM差异(如果有)?

Can an expert share the internal JVM difference, if there is one?

谢谢。

推荐答案

阵列从Java开始就已经出现了,而varargs是最近新增的。因此,许多旧代码仍然很乐意使用数组。

Arrays have been around from the beginning of Java, while varargs are a fairly recent addition. Thus a lot of older code still happily uses arrays.

另请注意,使用显式数组参数调用泛型vararg方法可能会产生与预期不同的行为:

Note also that calling a generic vararg method with an explicit array parameter may silently produce different behaviour than expected:

public <T> void foo(T... params) { ... }

int[] arr = {1, 2, 3};

foo(arr); // passes an int[][] array containing a single int[] element

因此 - 除了需要付出很多努力才能获得明显的好处 - 用varargs替换传统的数组参数并不总是可取的。

Thus - apart from requiring a lot of effort for no clear benefit - it is not always desirable to replace legacy array parameters with varargs.

更不用说你不能的情况,因为方法参数列表中的数组后面还有另一个参数:

Not to mention the cases when you can't, because there is another parameter after the array in the method parameter list:

public void foo(String[] strings, String anotherParam) { ... }

重新排序参数可能在技术上解决了这个问题,但它破坏了客户端代码。

Reordering the parameters may technically solve this, however it breaks client code.

更新:有效的Java第二版。版本,第42项:明智地使用varargs 更详细地解释了这一点,并给出了一个具体的例子: Arrays.asList()在Java5中被改装为有vararg参数,无意中打破了很多现有的代码可能会在使用这个(现在过时的)成语打印数组时引起惊喜:

Update: Effective Java 2nd. Edition, Item 42: Use varargs judiciously explains this in more details, giving also a concrete example: Arrays.asList() was retrofitted in Java5 to have vararg parameters, which inadvertently broke a lot of existing code may cause surprises when using this (now obsolete) idiom to print an array:

System.out.println(Arrays.asList(myArray));

Update2:仔细检查了源代码,并说明问题发生了使用基本类型数组,例如 int [] 。在varargs之前,代码如下:

Update2: Double checked the source, and it says that the problem occurrs with arrays of primitive types, such as int[]. Before varargs, code like this:

int[] digits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };
System.out.println(Arrays.asList(digits));

会发出编译错误,因为只有引用类型的数组可以转换为列表。由于varargs和改装 asList ,上面的代码编译时没有警告,意外的结果就像[[I @ 3e25a5]

would emit a compilation error, because only arrays of reference types could be converted to a List. Since varargs, and retrofitting asList, the code above compiles without warnings, and the unintended result is something like "[[I@3e25a5]".

这篇关于Java varags方法参数列表与数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆