Integer源码解析

1. 示例代码

接下来,以一段代码来解释Integer的装箱与拆箱

public class IntegerTest {
    public static void main(String[] args) {
		//装箱
        Integer obj=1;
		//拆箱
        int result=obj*2;

		//不在-128~127之间,结果为false
        Integer a=200;
        Integer b=200;
        System.out.println(a==b);
        //在-128~127之间,结果为true
        Integer c=100;
        Integer d=100;
        System.out.println(c==d);

    }
}

然后对这段代码进行编译和反编译:

javac IntegerTest.java
javap -c IntegerTest

反编译结果为:

Compiled from "IntegerTest.java"
public class grammer.integer_test.IntegerTest {
  public grammer.integer_test.IntegerTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: aload_1
       6: invokevirtual #13                 // Method java/lang/Integer.intValue:()I
       9: iconst_2
      10: imul
      11: istore_2
      12: sipush        200
      15: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      18: astore_3
      19: sipush        200
      22: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: astore        4
      27: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
      30: aload_3
      31: aload         4
      33: if_acmpne     40
      36: iconst_1
      37: goto          41
      40: iconst_0
      41: invokevirtual #23                 // Method java/io/PrintStream.println:(Z)V
      44: bipush        100
      46: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      49: astore        5
      51: bipush        100
      53: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      56: astore        6
      58: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
      61: aload         5
      63: aload         6
      65: if_acmpne     72
      68: iconst_1
      69: goto          73
      72: iconst_0
      73: invokevirtual #23                 // Method java/io/PrintStream.println:(Z)V
      76: return
}

2. 结果分析

2.1 装箱与拆箱的底层实现

由反编译的结果可以看出来:

  1. 装箱: 底层调用的是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);
    	}
    
  2. 拆箱: 底层调用的是Integer.intValue()方法
    public int intValue() {
        return value;
    }
    
2.2 使用==比较Integer结果为什么会有差异?

从示例代码可以看出:a和b都是200的时候,使用双等号 比较结果是false;而c和d都是1的时候,使用 双等号比较结果是true。
那到底是为什么呢?原因很简单,Integer内部缓存了-128~127之间的Integer对象,当处于这个范围时会直接返回缓存对象;而超过这个范围,是重新创建对象.众所周知,==比较的是内存地址.缓存对象都是同一个,那内存地址自然也是一致的.

以上是结论,那么依据是什么呢?依据就是装箱的代码:

    public static Integer valueOf(int i) {
    //如下IntegerCache定义:IntegerCache.low=-128,而IntegerCache.high=127
        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() {}
    }

当装箱时,如果值在-128~127,那么直接返回IntegerCache.cache(i + (-IntegerCache.low))的对象,否则new一个Integer

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值