只是想知道为什么在方法参数是 int 类型的情况下可以将 Integer 作为参数传递,反之亦然?
public class Salmon {
public static Integer foo(Integer a, int b){
return a - b;
}
public static void main(String[] args) {
Integer a = 10;
int b = 1;
foo(b, a);
}
}
最佳答案
这是自动装箱和自动拆箱。基本上,编译器会适本地调用 Integer.valueOf()
或 x.intValue()
。
确切的机制实际上并未指定,但规范的相关部分是 5.1.7 and 5.1.8 .
关于java - 将整数传递给 int,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4052085/