根据Java Tutorial ,
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
- Passed as a parameter to a method that expects a value of the corresponding primitive type.
- Assigned to a variable of the corresponding primitive type.
为什么会发生这种情况下的拆箱?
char l = 0;
int arr[] = new int[]{1,2,3};
System.out.println(arr[new Integer(1)]);
在这种情况下,这些事情会在哪里发生?是否有管理数组中元素访问的底层方法?或者 [] 是否暗示某种变量?
最佳答案
JLS 15, §15.10.3关于这一点非常清楚:
...
The index expression undergoes unary numeric promotion (§5.6). The promoted type must be
int
, or a compile-time error occurs....
类似的段落可以在旧的 JLS 中找到,例如JLS 8, §15.10.3 .
关于java - 为什么会出现这种情况下拆箱的情况呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65372253/