java Byte, Boolean, Integer, Short 比较运算符 == 误区

Java中的Byte, Boolean, Integer, Short等基本类型的封装类在使用`==`进行比较时,可能会涉及到对象地址的比较。对于值在[-128, 127]范围内的Integer,由于Integer类内部的静态缓存,相同值的Integer对象会共享同一个实例。同样,Byte和Short也有类似机制。理解这一细节有助于避免比较时的误区。" 119971042,8443725,skynet源码解析:lua服务启动流程,"['skynet框架', 'lua编程', '服务管理', '源码解析']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java对象的比较运算符"=="对象地址比较,但基本数据类型,如Byte, Boolean, Integer等基本类型的封装类在使用==时,有些细节需要注意,如以下代码:

public static void main(String []args) {
		Integer i1 = 1;
		Integer i2 = 1;
		Integer i3 = new Integer(1);
		System.out.println(i1 == i2); // true,i1 和 i2是一个对象
		System.out.println(i1 == i3); // false,i1 和 i3 不是一个对象
    }

为何i1和i2的地址一样呢, 我们可以看一下Integer的valueOf实现

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() {}
    }

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

也就是说,对于值在[-128, 127],127这个上限可以设置,Integer是有静态缓存的,如果采用valueOf得到的Integer对象,相同的值可能是一个对象。

同样的,Byte, Boolean, Short 也是一样的,具体cache范围可看jdk的实现。

加入我们:http://www.duoceshi.com/team.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值