public static void main(String[] args) {
Integer A =128;
Integer B = 128;
System.out.println(A == B); //false
Integer C =64;
Integer D = 64;
System.out.println(C == D); //true
}
利用包装类的缓存机制,在-128到127这个范围期间的数,返回的是缓存(内存)中的引用,而不是创建一个新的对象。
public static void main(String[] args) {
int A =128;
Integer B = 128;
System.out.println(A == B); //true
Integer C =64;
Integer D = 64;
System.out.println(C == D); //true
}
这里Integer会自动拆箱成int类型用其数值与int基本数据类型值进行比较