自动拆箱引起的异常
首先思考,下边的程序会输出什么?
package com.example.a;
public class Demo {
public static void main(String[] args) {
Integer a = null;
System.out.println(2 == a);
}
}
结果:
Exception in thread "main" java.lang.NullPointerException
at com.example.a.Demo.main(Demo.java:6)
因为是基本类型与包装类型进行比较,此时会将包装类型自动拆箱,调用a.intValue()获得其基本类型的值,但a是null,所以报空指针异常。
案例一
package org.example.a;
public class Demo {
public static void main(String[] args) {
int int1 = 12;
int int2 = 12;
Integer integer1 = new Integer(12);
Integer integer2 = new Integer(12);
System.out.println("int1 == int2 : " + (int1 == int2));
System.out.println("int1 == integer1 : " + (int1 == integer1));
System.out.println("integer1 == integer2 : " + (integer1 == integer2));
}
}
运行结果:
int1 == int2 : true
int1 == integer1 : true
integer1 == integer2 : false
Integer是int的封装类,当Integer与int进行==比较时,Integer就会拆箱成一个int类型,所以还是相当于两个int类型进行比较,这里的Integer不管是直接赋值,还是new创建的对象,只要跟int比较就会拆箱为int类型,所以就是相等的。
案例二
package org.example.a;
public class Demo {
public static void main(String[] args) {
Integer integer1 = new Integer(12);
Integer integer2 = 12;
System.out.println("integer1 == integer2 : " + (integer1 == integer2));
}
}
运行结果:
integer1 == integer2 : false
两个都是对象,"=="运算符比较的是对象的引用,而不是对象的值。所以,尽管它们的值相等,但是它们是两个不同的对象,所以结果会是false。如果想比较两个Integer对象的值是否相等,应该使用equals()方法。
缓存问题解析
测试代码:
package org.example.a;
public class Demo{
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b);
System.out.println(c == d);
}
}
运行结果:
true
false
原因分析:
Integer缓存,原理如下。valueOf源码:
//private static class IntegerCache {
// static final int low = -128;
// static final int high = 127;
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
- 当i的值位于[-128,127]的时候,会直接返回Integer缓存数组中相应对象的引用,这种缓存行为是由Integer类内部的IntegerCache实现的。当使用自动装箱创建Integer对象时,如果值在-128到127之间,它会从缓存中获取已有的对象引用,而不是创建新的对象。如果i大于127或小于-128,会重新创建一个Integer实例,并返回。
- 那么第一条式子a和b的值都在缓存范围内,因此他们指向同一个对象,因此返回true。c和d的值不在范围内,都是通过new创建出来的,因此不是同一个对象,返回false。