Integer.ValueOf(int)和new Integer(int)性能比较
JDK的源码,看到Integer.ValueOf(int)里面做了优化
public static Integer valueOf(int i) {final int offset = 128;if (i >= -128 && i <= 127) { // must cachereturn IntegerCache.cache[i + offset];}return new Integer(i);}private static class IntegerCache {private IntegerCache() {}static final Integer cache[] = new Integer[-(-128) + 127 + 1];static {for (int i = 0; i < cache.length; i++)cache[i] = new Integer(i - 128);}}
public static void main(String[] args) {Integer a = 100;Integer b = 100;System.out.println(a == b);Integer c = new Integer(100);Integer d = new Integer(100);System.out.println(c == d);}