2

I got an Java question during my interview as below

public static boolean isSame(Integer a, Integer b){
 return a==b;
}

public static void main(String[] arg){
 int i=0;
 for(int j=0;i<500;++i,++j){
     if(isSame(i,j)){
         continue;
     }
     else break;
     }
}

The question is "i=?" at last.

I thought i would be 500 at last. But when I tried it in Eclipse i=128!

So I was wondering what is happening here.

Thanks

Zach_Cat
  • 59
  • 5

3 Answers3

7

Comparing two Integer objects using == will only return true if they are the same object (ie the same exact instance), ie regardless of their value.

However, the values -128 to 127 are cached, so auto-boxing these values (which is occurring when you pass an int in as an Integer parameter) always returns the same instance of Integer for a given value.

Values outside this range always result in a new instance of Integer being created.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    [JLS ยง5.1.7](http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7) guarantees that -128..127 are cached but also states that "Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K." So better not rely on values outside are not. โ€“ zapl Jan 24 '14 at 03:35
  • So the correct answer to this OP's interview question is "Implementation dependent" (possible with the additional note "but at least 128" - 128 because it's one more than the max cached value) โ€“ Erwin Bolwidt Jan 24 '14 at 03:39
  • @ErwinBolwidt Yes. The question is either badly designed or actually very good. [Java 7](http://stackoverflow.com/questions/15052216/how-large-is-the-integer-cache) also seems to have added setting to change the cache size. โ€“ zapl Jan 24 '14 at 03:44
1

source code:

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

public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
zhixiu1018
  • 21
  • 1
0

This is because of auto boxing.

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

To know more about auto boxing refer here.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7

SANN3
  • 9,459
  • 6
  • 61
  • 97