例1:缓存
Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e= 321; Integer f= 321; Long g = 3L; System.out.println(c == d); //1 System.out.println(e == f); //2 System.out.println(c == (a+b)); //3 System.out.println(c.equals(a+b));//4 System.out.println(g == (a+b)); //5 System.out.println(g.equals(a+b)); //6
输出结果code
true false true true true false
1.包装类比较,不会自动拆包,可是Integer中会有一个cache 存储-128到127的数,因此c与d的地址值相同。
2.地址值比较,没用到cache
3.当 '=='时,右侧发生自动拆包,因此实际上是int值在比较
4.a+b 时拆包成int,传入Integer的equals方法进行自动装包。equals方法内是值比较。
5.会拆包成基础数据类型比较
6.包装类的equals 会判断类型,Long.equals(Object object)中判断类型不符合,返回false。源码
例2:基础
Long a = 1L; Integer b = 1; System.out.println(a.equals(1)); //7 System.out.println(a.equals(1L)); System.out.println(a.equals(b));
输出object
false true false
看包装类源码会发现比较时会先去判断类型是否相同。
7.a.equals(1)时,int 1 装包成Integer,天然和Long不一样类型。数据类型
public boolean equals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } return false; }
总结:
当使用自动拆包/装包时,包装类之间比较并不会自动拆包,是地址比较,其中还有缓存会影响结果。
用包装类的equals方式比较时,因为包装类并不会自动去转换类型,因此类型不一样时,即便值相同,也会返回false。因此在用包装类比较数值时,不要用'==',用equals方法时要注意类型相同,或者直接用基础数据类型比较。方法