如有写错的地方请多多指教
public class IntegerAndLongPoint01 {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
Long h = 2L;
Long l = 3L;
Long m = 128L;
Long n = 128L;
Long o = 127L;
//java中会有一个Integer缓存池,缓存的大小是:-128~127
//Long为常量且Long的大小在-128~127之间,两个Long指向同一常量内容
/**
* 使用 == 比较的情况:
* 如果比较Integer和Long,默认比较的是地址值
* Java的Integer维护了从-128~127的缓存池
* 如果比较的某一边有操作表达式(例如:a+b),那么比较的是具体数值
*/
System.out.println(c == d);//T
System.out.println(e == f);//F
System.out.println(g == l);//T
System.out.println(m == n);//F
System.out.println(g == (a + b));//T
System.out.println(c == (a + b));//T
/**
* 使用equals()的情况:
* 无论是Integer还是Long中的equals方法,默认比较的都是数值
* Long的equals方法先判断传递进来的是不是Long类型,JDK的默认实现:会判断是否是Long类型
* ----------------- a + b自动装箱的是Integer类型 -------------------------------
*/
System.out.println(c.equals(a + b));//T
System.out.println(g.equals(a + b));//F
System.out.println(g.equals(a + h));//T
System.out.println(e.equals(f));//T
System.out.println(m.equals(n));//T
System.out.println(m.equals(a + o));//T
}
}