学习记录并且简单分析

学习记录并且简单分析

记录学习遇到的困难,一个小白!

本人基本没有读过源码,纯小白一个,写这篇文章纯属是为了提高自己!!!希望各位积极指出错误。

好奇的我翻看了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);
 }

首先参数解释:

static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;

high没有赋值,所以我们继续跟踪到high属性

 static {
     int h = 127;
     //启动时如果指定-Djava.lang.Integer.IntegerCache.high=XXX(自己输入一个数据)参数
     //将会执行下面的语句,则动态的设置h是127还是设置的输入数据
     String integerCacheHighPropValue =
         //该方法是通过传递一个key,获取启动时手动设定的数值
         VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
     if (integerCacheHighPropValue != null) {
         try { 
            
             h = Math.max(parseInt(integerCacheHighPropValue), 127);
         	 //	
             // public static int max(int a, int b) {
      		//	  return (a >= b) ? a : b;
    		//	}
             
             // Maximum array size is Integer.MAX_VALUE
             //32位系统2147483647=2^32
             h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
            
         } catch( NumberFormatException nfe) {
             // If the property cannot be parsed into an int, ignore it.
         }
     }
   	//找到high属性的值由h局部属性赋值
     high = h;

     // Load IntegerCache.archivedCache from archive, if possible
     VM.initializeFromArchive(IntegerCache.class);
     
     int size = (high - low) + 1;//可以存256个数。
     //至于为啥是256?这里我不讲述,建议自己去搜索下计算机存放数据相关的概念!

     // Use the archived cache if it exists and is large enough
     if (archivedCache == null || size > archivedCache.length) {
         Integer[] c = new Integer[size];
         int j = low;
        //c.length=256
        //j=-128
         for(int i = 0; i < c.length; i++) {
             c[i] = new Integer(j++);
         }
         //数组c对应256个数,c[0]对应-128,c[256]对应127
         //很巧妙
         archivedCache = c;
     }
     cache = archivedCache;
     // range [-128, 127] must be interned (JLS7 5.1.7)
     //断言,如果最高值小于127抛出异常
     assert IntegerCache.high >= 127;
 }


现在我们再回到

public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high)
         //前面说过设计很巧妙
         //i为什么要加上-(-128)?你理解没!
         //原因是因为cache数组的第0位保存的是-128!
         return IntegerCache.cache[i + (-IntegerCache.low)];
     return new Integer(i);//如果不在该范围,则创建实例
 }

现在我们就明白了如果实参在-128到127之间,就会直接去从静态方法区拿实例对象,这么做是为了减少开销。

下面验证

Integer int_instance1 = Integer.valueOf("100");
Integer int_instance2 = Integer.valueOf("100");
System.out.println(int_instance1 == int_instance2);

运行结果是true


Integer int_instance1 = Integer.valueOf("100");
Integer int_instance2 = Integer.valueOf("101");
System.out.println(int_instance1 == int_instance2);

运行结果是false

原因通过下面的表解释,简单的画了一下

Integer int_instance1 = Integer.valueOf("128");
Integer int_instance2 = Integer.valueOf("128");
System.out.println(int_instance1 == int_instance2);

结果是false

原文地址:https://www.cnblogs.com/cwhan/p/13944272.html