上两段代码:
private String print(Object... values) {
StringBuilder sb = new StringBuilder();
for (Object o : values) {
sb.append(o.toString())
.append(" ");
}
return sb.toString();
}
public void testPrintMethod(PrintStream out) throws IOException {
out.println(print(new Object[] {0, 2, 4, 6, 8, 10, 9, 7, 5, 3, 1}));
out.println(print(new Integer[] {0, 2, 4, 6, 8, 10, 9, 7, 5, 3, 1}));
out.println(print(new int[] {0, 2, 4, 6, 8, 10, 9, 7, 5, 3, 1}));
}
这三个输出是否应该一样呢?
事实上结果是这样子的:
0 2 4 6 8 10 9 7 5 3 1
0 2 4 6 8 10 9 7 5 3 1
[I@149d886
最后一个int数组没能转换成Object数组,而是整体被转换成了一个Object。
我不知道为什么,java没有对成员进行Autoboxing,许是怕引起其他的混淆吧。
暂且记录下来。