java - 为什么 Java 不将 int[] 自动装箱为 Integer[]?

标签 java autoboxing

我有这个方法:

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/

相关文章:

java - 了解双重自动装箱

java - 为什么我的位置是0?什么时候应该是1

Java : Getting NullPointerException while parsing the List and setting it inside a Array

Java 转换 BufferedImage "Subimage"由 4 个点定义

java - FileReader如何构造FileInputStream和Input Reader?

java - 为什么以下代码会自动装箱为错误的类型并进行编译?

java - 如何在Java中实现方法链?

java - 从 Java 中的 arrayList 中删除最后 n 个元素

java - 开箱贵吗?

Java反射和自动装箱