gpt4 book ai didi

java - 为什么自动装箱在数组中不起作用?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:57 24 4
gpt4 key购买 nike

我不明白为什么这段代码没有编译:

 Object[] arr = new int[3];

我不需要此代码即可工作。我只想了解自动装箱在这种情况下不起作用的原因

最佳答案

来自 this回答Why do we use autoboxing and unboxing in Java? , 我会引用您要回答的问题的相关详细信息:

Primitive variables aren't interchangeable in the same way, neither with each other, nor with Object. The most obvious reason for this (but not the only reason) is their size difference. This makes primitive types inconvenient in this respect, but we still need them in the language (for reasons that mainly boil down to performance).

因此,另一方面,适合您的是:

Object[] arr = new Integer[3];

关于java - 为什么自动装箱在数组中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45918075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com