3

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?

Community
  • 1
  • 1
Anand
  • 20,708
  • 48
  • 131
  • 198

3 Answers3

8

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.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • The JLS says *at least* values -128 to 127 are cached, not *up to* that range. It even says in the discussion paragraph: "Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. ... This would allow (but not require) sharing of some or all of these references." – yshavit Sep 06 '12 at 19:48
  • @yshavit changed to `between` – Amit Deshpande Sep 06 '12 at 19:50
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        */
Tony K.
  • 5,535
  • 23
  • 27
1

[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.

poison
  • 189
  • 3