Behavior of Wrapper class in Java [duplicate]

这一生的挚爱 提交于 2019-12-12 14:21:04

问题


Possible Duplicate:
Why does 128==128 return false but 127==127 return true in this code?

The below piece of Java code returns true

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

So the way we have concept of String literal constant pool in Java, do we have the similar concept in case of wrapper classses in Java also?


回答1:


Java also has Integer pool for small integers between -128 to 127 so it will behave same for Integer also similar to String Constant pool

You will find below code in Integer class

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

Also as Stated in below answer by poison:

Chapter 5. Conversions and Promotions

if the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.




回答2:


The pools for objects are an artifact of the VM and/or runtime environment. They are likely to be there for performance reasons, but you should never rely on them. Use .equals().

The JLS specifies the boxing behavior, and as was pointed out to me in the comments, this is partially implemented in the Integer class itself; however, another interesting note is that even the size of this pool is tunable by a VM argument. From 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        */



回答3:


[5.1.7. Boxing Conversion] http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

But in general it would be foolish to rely on this as you'd first have to check if the number is in the cached range and then conditionally use == or equals(). Use == for primitive types, Class and enum, equals for everything else.



来源:https://stackoverflow.com/questions/12306853/behavior-of-wrapper-class-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!