我有这个方法:
void stuff(Object[] array){
// .. do stuff with the array
}
并尝试这样做:
stuff(intArray);
它无法编译。我必须手动转换我的 int[]
到Integer[]
.
自动装箱不是应该帮我处理这些事情吗?为什么不呢?
而且事实证明 Java 语言本身并不能执行此操作,为什么 JDK 中没有实用程序来执行此操作?这不是我们拥有标准库的原因吗?
最佳答案
自动装箱不适用于数组,自动装箱使用包装类的 static
valueOf()
方法将基元转换为其包装对象
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
关于java - 为什么 Java 不将 int[] 自动装箱为 Integer[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857704/