public static void main(String[] args) {
for (int i = 0; i < 150; i ) {
Integer a = i;
Integer b = i;
System.out.println(i " " (a == b));
}
}
0 true 1 true 2 true 3 true ... 126 true 127 true 128 false 129 false 130 false ...
public static void main(String[] args) {
Map<Integer, Integer> mapA = new HashMap<>();
Map<Integer, Integer> mapB = new HashMap<>();
for (int i = 0; i < 150; i ) {
mapA.put(i, i);
mapB.put(i, i);
}
for (int i = 0; i < 150; i ) {
System.out.println(i " " (mapA.get(i) == mapB.get(i)));
}
}
0 true 1 true 2 true 3 true ... 126 true 127 true 128 false 129 false 130 false ...
Integer a = 1;
Integer a = Integer.valueOf(1);
public static void main(String[] args) {
for (int i = 0; i < 150; i ) {
Integer a = i;
Integer b = i;
System.out.println(i " " (a == b));
}
}
new Integer(1) == new Integer(1);
for(int i=0;i<150;i ){
Integer a=i;
Integer b=i;
System.out.println(a " " b " " System.identityHashCode(a) " " System.identityHashCode(b));
}
0 0 762119098 762119098 1 1 1278349992 1278349992 2 2 1801910956 1801910956 3 3 1468253089 1468253089 ... 126 126 1605164995 1605164995 127 127 1318497351 1318497351 128 128 101224864 479240824 129 129 1373088356 636728630 130 130 587071409 1369296745 ...
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) 1];
int j = low;
for(int k = 0; k < cache.length; k )
cache[k] = new Integer(j );
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
for (int i = 0; i < 150; i ) {
Integer a = i;
Integer b = i;
System.out.println(i " " (a.equals(b)));
}
END 十期推荐 【251期】面试官:谈谈你对零拷贝的理解~ 【252期】运行时常量池的一道面试题(JDK8环境) 【253期】面试官:熟悉Docker操作吗?说几个常用的Docker命令吧 【254期】面试官:来谈谈微服务组件Feign的工作原理吧 【255期】面试官:Mybatis是如何运用设计模式的? 【256期】面试官常考的 21 条 Linux 命令 【257期】面试官:谈谈你对Java线程安全与不安全的理解 【258期】今日头条的面试题:LRU原理和Redis实现 【259期】面试官:Spring事务失效的场景有哪些?如何解决? 【260期】Java线程池,这篇能让你和面试官聊了半小时 ? ~