interger判断相等,integer和integer用==比较

Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
System.out.println(c == d);
System.out.println(e == f);

输出 true false

 

Integer为对象判断是否相等还是使用equals最靠谱,

int为基本类型,判断是否相等就是可以使用==.

 

原因:

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
      for(int i = 0; i < cache.length; i++) {
             cache[i] = new Integer(i – 128);
      }
}

这是源码,也就是说cache中已有-128到127,不在这范围的会新new ,这时可以理解比较的是内存地址,也就是是不是同个一对象.

所以说当Integer的值不在-128到127的时候使用==方法判断是否相等就会出错,在这个范围之内的就会没有问题!!!

转载于:https://www.cnblogs.com/songshu120/p/7279327.html

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注