Java坑人面试题之自动装箱和拆箱后的引用数据类型和基本数据类型的计算

在Java1.5以后的版本中,引用类型Integer和基本数据类型int之间是可以通过自动装箱和拆箱进行计算的

例如: Integer in = 1; //it means  Integer in = new Integer(1);

in = in +1;// Integer type in convert to int type  via function Integer.intValue()

//in+1 =  in.intValue(1) + 1;

So, in = 2; //auto picking : pass Integer(2) = Interger in;

public static void functin2() {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);//false
System.out.println(i.equals(j));//true

Integer a = 500;
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true


//when the data in the scope of byte(127),JVM will not create new object
Integer aa = 50;
Integer bb = 50;
System.out.println(aa==bb);//true
System.out.println(aa.equals(bb));//true

}

猜你喜欢

转载自www.cnblogs.com/robert-zhang/p/9143111.html