Integer 内部实现

 1 public static void main(String[] args) {
 2         Integer in1 = 128;
 3           Integer in2 = 128;
 4           System.out.println(in1 == in2 );
 5 }
 6 会发现该代码在复值不同的时候返回的结果是不相同。
 7 以上的代码要分析原理的话那么需要进行源码的调试,那么就需要关联源码。
 8 public static Integer valueOf(int i) {
 9         if(i >= -128 && i <= IntegerCache.high)
10             return IntegerCache.cache[i + 128];
11         else
12             return new Integer(i);
13     }
14 如果指定值在-128~127之间那么直接返回的是该对象已经缓存好的Integer对象,那么同值的Integr是同一个对象,如果不在那么直接new出新的对象直接返回一定不等。
原文地址:https://www.cnblogs.com/friends-wf/p/3718727.html