源码级别理解java拆箱装箱过程

知道字节码吗,字节码都有哪些?比较下面代码x == y都经过了哪些过程

public class interview {
    public static void main(String[] args) {
        Integer x = 5;
        int y = 5;
        System.out.println(x == y);
    }
}

通过idea的工具jClasslib插件查看main方法的code,发现:

image-20210913142135732

然后我们去Integer中查看Valueof和intValue方法

valueOf
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
    public static Integer valueOf(int i) {
        // 这里调用了
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
// Integer的内部类
private static class IntegerCache {
    // 定义最小值为-128
    static final int low = -128;
    static final int high;
    // 定义一个缓存数组
    static final Integer cache[];

    static {
        // high value may be configured by property
        // high 的默认值为127
        int h = 127;
        // 从vm中拿到high的数值,这个数值可以通过vm参数配置
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        // 如果拿到了这个值,说明进行了vm配置
        if (integerCacheHighPropValue != null) {
            try {
                // 把这个值转化为int类型
                int i = parseInt(integerCacheHighPropValue);
                // 这个值得最小值要大于等于127
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                // 这个值得最大值是2147483518,也就是整个cache数组的长度为Integer的最大值
                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数组的长度
        cache = new Integer[(high - low) + 1];
        int j = low;
        // 为这个cache填充值
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        // 断言这个值得最大值大于等于127
        assert IntegerCache.high >= 127;
    }
	
    // 私有化构造方法,jvm级别单例
    private IntegerCache() {}
}
intValue
// 返回了int类型的一个值value
public int intValue() {
    return value;
}
private final int value;

// 这个vlaue在创建Integer对象的时候
public Integer(int value) {
    this.value = value;
}
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    // 在一步调用了new Integer()的方法
    return new Integer(i);
}
总结
  1. 在创建一个integer的对象的时候,回调用valueOf方法
    1. valueOf方法回去调用IntegerCache中的一个值
      1. IntegerCache
        1. 如果没被初始化,则优先从jvm参数中读取值进行初始化
        2. 如果初始化过了则直接拿到值
      2. 然后调用new Integer(i)方法
  2. 然后把这个对象放入栈中的局部变量表
  3. 然后判断的时候,调用intValue方法,然后int类型的值
  4. 两个int类型的值进行判断

博客首先发布于:

https://lvxiaoyi.top/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值