Java varargs扩大的奇怪行为

时间:2013-12-04 08:48:46

标签: java java-ee

我正在阅读包含var-arg参数的方法......

声明以下内容是不合法的 (因为var-arg被隐式地作为数组传递,即将是一个重复的方法)

void m1(int... i) {}
void m1(int[] i) {}

但流动是合法的宣告:

void m1(int... i) {}
void m1(Integer... i) {}

但是,这些方法只能在相同类型的数组中合法调用 像:

m1(new Integer[]{});
m1(new int[]{});

m1(2, 3);                           //Compile ERR: The method m1(int[]) is ambiguous                
m1(new Integer(2), new Integer(3)); //Compile ERR: The method m1(int[]) is ambiguous
使用以下命令调用

void m1(int... i){}

m1((byte)1,(short)2);     
m1(new Byte((byte)1), new Byte((byte)1));

//Both compile fine and the method 
//void m1(int... i) {} is invoked

我的问题,如果(byte)1,(short)2new Byte((byte)1), new Byte((byte)1)传递为int[]

为什么m1(2, 3)m1(new Integer(2), new Integer(3));的行为不一样?

3 个答案:

答案 0 :(得分:1)

正如您所读到的那样,您可能知道var-arg参数进程是运行时的通用参数进程,并且在使用Generic读取那些here时以及对于您的后续查询时存在一些限制:

m1((byte)1,(short)2);//for this it will automatically wrap your digits in non primitive class object read boxing conversation
m1(new Byte((byte)1), new Byte((byte)1));//for this it is working as you are creating non primitive objects

Boxing Conversation

答案 1 :(得分:1)

记住加宽节拍拳击击败Var-Args

(byte)1,(short)2不能加宽为int,int,然后是Integer,Integer(Widen Then Box) - 对于JVM来说是昂贵或沉重的。 short和byte可以很容易地扩展为int,因此int ...被调用。

new Byte((byte)1),new Byte((byte)1)不能强制转换为(Integer,Integer),即Unbox to byte,然后加宽为int,再将box加到Integer(Widen then Box) - Heavy 。但它可以很容易地取消装箱到字节并扩大到int(Box然后Widen并不重)所以int ...被调用。

在上面两种情况下,将调用int []。

m1(2,3)可以装箱到整数[]或传递给int [] - 歧义 m1(new Integer(2),new Integer(3))可以传递给Integer []或unboxed到int。

答案 2 :(得分:0)

Byte不会延伸Integer。因此,通过执行void m1(Integer i) {}(使用或不使用varargs)来调用方法m1(new Byte(1))是无效的。

它适用于intbyte,因为byte可以作为int引用。

相关问题