Just wondered why is it possible to pass Integer as argument where method parameter is of int type and vice versa?
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);
}
}
This is auto-boxing and auto-unboxing . Basically the compiler puts in calls to Integer.valueOf()
or x.intValue()
appropriately.
The exact mechanism isn't actually specified, but the relevant sections of the spec are 5.1.7 and 5.1.8 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.