有关整数缓冲区的问题

有关整数缓冲区的问题

问题:

  1. 用构造函数装箱,比较结果为false;
  2. 采用自动装箱,100时比较结果为true,200时结果比较为false。
  3. 问题实例代码:
public class IntegerLei {
    public static void main(String[] args) {
        // 1.用构造函数装箱
        Integer integer1 = new Integer(100);
        Integer integer2 = new Integer(100);
        System.out.println(integer1==integer2);

        // 2.用自动装箱
        Integer integer3 = 100;
        Integer integer4 = 100;
        System.out.println(integer3==integer4);

        integer3 = 200;
        integer4=200;
        System.out.println(integer3==integer4);

    }
}

/*运行结果:
    false
    true
    false
*/

原因:

  1. 采用XJad软件我们可以把编译好的.class文件反编译回java文件,可以发现自动装箱操作其实是采用了Integer.valueof()方法;

    // Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
    // Jad home page: http://kpdus.tripod.com/jad.html
    // Decompiler options: packimports(3) fieldsfirst ansi space 
    // Source File Name:   IntegerLei.java
    
    import java.io.PrintStream;
    
    public class IntegerLei
    {
    
    	public IntegerLei()
    	{
    	}
    
    	public static void main(String args[])
    	{
    		Integer integer1 = new Integer(100);
    		Integer integer2 = new Integer(100);
    		System.out.println(integer1 == integer2);
    		Integer integer3 = Integer.valueOf(100);
    		Integer integer4 = Integer.valueOf(100);
    		System.out.println(integer3 == integer4);
    		integer3 = Integer.valueOf(200);
    		integer4 = Integer.valueOf(200);
    		System.out.println(integer3 == integer4);
    	}
    }
    
  2. so:问题就出现valueof的源码。源码如下:

    @IntrinsicCandidate
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    

    发现:valueOf(int i)方法中的参数值在IntegerCache.lowIntegerCache.high之间时,会返回数组IntegerCache.cache[]中的值。

    为查看三者的值查看IntegerCache类的源码如下:

        private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final Integer[] cache;
            static Integer[] archivedCache;
    
            static {
                // high value may be configured by property
                int h = 127;
                String integerCacheHighPropValue =
                    VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
                if (integerCacheHighPropValue != null) {
                    try {
                        h = Math.max(parseInt(integerCacheHighPropValue), 127);
                        // Maximum array size is Integer.MAX_VALUE
                        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;
    
                // Load IntegerCache.archivedCache from archive, if possible
                CDS.initializeFromArchive(IntegerCache.class);
                int size = (high - low) + 1;
    
                // 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;
                    for(int i = 0; i < c.length; i++) {
                        c[i] = new Integer(j++);
                    }
                    archivedCache = c;
                }
                cache = archivedCache;
                // range [-128, 127] must be interned (JLS7 5.1.7)
                assert IntegerCache.high >= 127;
            }
    
            private IntegerCache() {}
        }
    

    从源码中可以看出IntegerCache.low==-128IntegerCache.high==127,同时可以看出数组cache[]大小为256,存放的元素为{-128,…,127}。

  3. 回到valueof的源码,当valueOf(int i)中的整数值在[-128,127]之间,返回的是数组cache[]中的元素,当超过这个范围,调用new Integer()​构造函数进行装箱。

XJad反编译软件下载链接

XJad-class反编译工具 https://www.aliyundrive.com/s/S9PZ1G7iWti 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值