public static void main(String[] args){
Integer a=100;//当值为-128-127的时候不会new对象
Integer b=100;
Integer c=150;//值会new对象
Integer d=150;
System.out.println(a==b);//ture
System.out.println(c==d);//false
Integer e =new Integer(100);
Integer f=100;
int g=100;
System.out.println(e==f);//false
System.out.println(e==g);//ture
System.out.println(f==g);//true
}总结:主要是Integer a=?变量a在生成的时候根据数值的不同会采用不同的方式。在-128到127时直接在栈中产生,而不在这个区间的时候在堆上产生,
应对的方式是都用equals()进行比较。
转载请注明原文地址: https://ju.6miu.com/read-1124164.html