Java中包装类的行为

Java中包装类的行为,java,wrapper,Java,Wrapper,可能重复: 下面的一段Java代码返回true Integer i1=1; Integer i2=1; System.out.println(i1==i2); 因此,我们在Java中有字符串文字常量池的概念,在Java中的包装器类中也有类似的概念吗?Java也有整数池,用于-128到127之间的小整数,因此对于整数,它的行为也与字符串常量池类似 您将在Integer类中找到以下代码 private static class IntegerCache { static final in

可能重复:

下面的一段Java代码返回true

Integer i1=1;
Integer i2=1;
System.out.println(i1==i2);

因此,我们在Java中有字符串文字常量池的概念,在Java中的包装器类中也有类似的概念吗?

Java也有整数池,用于
-128到127之间的小整数,因此对于整数,它的行为也与字符串常量池类似

您将在
Integer
类中找到以下代码

private static class IntegerCache {
    static final int high;
    static final Integer cache[];

    static {
        final int low = -128;

        // high value may be configured by property
        int h = 127;
        if (integerCacheHighPropValue != null) {
            // Use Long.decode here to avoid invoking methods that
            // require Integer's autoboxing cache to be initialized
            int i = Long.decode(integerCacheHighPropValue).intValue();
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = Math.min(i, Integer.MAX_VALUE - -low);
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
    }

    private IntegerCache() {}
}
私有静态类整型缓存{
静态最终int高;
静态最终整数缓存[];
静止的{
最终整数下限=-128;
//高值可由属性配置
int h=127;
if(integerCacheHighPropValue!=null){
//在此处使用Long.decode以避免调用
//需要初始化Integer的自动装箱缓存
int i=Long.decode(integerCacheHighPropValue).intValue();
i=数学最大值(i,127);
//最大数组大小为整数。最大值为
h=数学最小值(i,整数最大值--low);
}
高=h;
缓存=新整数[(高-低)+1];
int j=低;
for(int k=0;k
也如以下毒药回答中所述:

如果要装箱的值p为true、false、一个字节或\u0000到\u007f范围内的字符,或一个介于-128和127(包括-128和127)之间的整数或短数字,则让r1和r2为p的任意两个装箱转换的结果。r1==r2总是这样


对象池是VM和/或运行时环境的工件。它们可能是出于性能原因而存在的,但您永远不应该依赖它们。使用.equals()

JLS指定了装箱行为,正如在注释中向我指出的,这部分是在Integer类本身中实现的;然而,另一个有趣的注意事项是,即使这个池的大小也可以通过VM参数进行调整。从Integer.java:

585       /**
586        * Cache to support the object identity semantics of autoboxing for values between
587        * -128 and 127 (inclusive) as required by JLS.
588        *
589        * The cache is initialized on first usage.  The size of the cache
590        * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
591        * During VM initialization, java.lang.Integer.IntegerCache.high property
592        * may be set and saved in the private system properties in the
593        * sun.misc.VM class.
594        */
585/**
586*缓存,支持之间值的自动装箱的对象标识语义
JLS要求的587*-128和127(含)。
588        *
589*缓存在第一次使用时初始化。缓存的大小
590*可由-XX:AutoBoxCacheMax=选项控制。
591*在VM初始化期间,java.lang.Integer.IntegerCache.high属性
592*可以设置并保存在
593*sun.misc.VM类。
594        */

[5.1.7.装箱转换]

被装箱的值p为true、false、一个字节或\u0000到\u007f范围内的字符,或一个介于-128和127(包括-128和127)之间的整数或短数字,然后让r1和r2成为p的任意两个装箱转换的结果。r1==r2总是这样

但一般来说,依赖于此是愚蠢的,因为首先必须检查数字是否在缓存范围内,然后有条件地使用==或equals()。
对于基元类型、类和枚举,使用==;对于其他所有类型,使用等于。

Integer
池对象,而不是JVMAh,这很有趣。所以,与字符串不太一样。JLS说至少缓存了128到127的值,不超过这个范围。它甚至在讨论段落中说:“理想情况下,装箱一个给定的原语值p,将始终产生一个相同的引用。在实践中,使用现有的实现技术,这可能不可行……这将允许(但不要求)共享部分或全部这些引用。”@yshavit在