只是想知道为什么可以将Integer作为参数传递,其中method参数是int类型,反之亦然?
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);
}
}
答案 0 :(得分:7)
这是自动装箱和自动装箱。基本上,编译器会适当地调用Integer.valueOf()
或x.intValue()
。
实际上并未指定确切的机制,但规范的相关部分为5.1.7 and 5.1.8。