Java 高阶 —— 相等性比较

1. 包装类

  • 所有的相同类型的包装类对象之间的值的比较,全部使用 equals 方法;

    • 对于 Integer a = ?,在 -128 到 127 范围内的赋值,Integer 对象是在 IntegerCache.cache 产生,便会复用已有对象,该区间内的 Integer 值可以直接使用 == 判断,但该区间外的所有数据,都会在堆上产生,并不会复用已有对象。
    Integer a = 100;
    Integer b = 100;
    System.out.println(a == b);
    System.out.println(a.equals(b));
    
    Integer c = 200;
    Integer d = 200;
    System.out.println(c == d);
    System.out.println(c.equals(d));
原文地址:https://www.cnblogs.com/mtcnn/p/9421121.html