比较
- 为什么第一个结果是true,第二个是false?
Integer a = Integer a = 1;
Integer b = 1;
Integer c = 500;
Integer d = 500;
System.out.print(a == b);
System.out.print(c == d);
上述代码返回结果为:true false
public static Integer valueOf(public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
IntegerCache.low = -IntegerCache.low = -128
IntegerCache.low = 127
整数赋给Integer型变量,会调用Interger.valueOf方法,-128到127返回的是IntegerCache中的缓存,其他值会new Integer(i)
- 字符串的equals 和 ==
hashcode() : 返回对象的hashcode(),通常实现为对象的内部地址转化为整数。
equals()返回true,则hashcode()一定相同。equals{}返回false,hashcode()不一定不同。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!