Doubt in AutoBoxing

Hi,

Can anybody explain why am i getting true in first case and false in second.

publicvoid method(Integer i1, Integer i2){

System.err.println(i1 == i2);

}

delMe.method(100, 100);// true

delMe.method(128, 128);// false

[444 byte] By [javapro99a] at [2007-11-27 5:46:15]
# 1
Operator == compares the object reference, so in case 1 it's the same instance of Integer you're comparing, in case 2 it's not.
quittea at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 2
> so in case> 1 it's the same instance of Integer you're comparing,> in case 2 it's not.whats the problem in 2nd case. its also same instance of integer right?
javapro99a at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 3
And thanks to auto-boxing, values in the range from -128 to 127 are cached and refer to the same instance. http://www.java-tips.org/java-se-tips/java.lang/introduction-to-autoboxing-3.html
quittea at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 4

Integers >= -128 && i <= 127 are being cached so these are pointing to the same object: hence == returns true for these cases.

Compare them like this:public void method(Integer i1, Integer i2){

System.err.println(i1.intValue() == i2.intValue());

}

prometheuzza at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 5
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.
YoGeea at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks a lot.
javapro99a at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 7
> Integers >= -128 && i <= 127 are being cached so> these are pointing to the same object: hence ==> returns true for these cases.Is the same rule hold good for other primitive wrappers? or there is separate range for them?
Satish_Patila at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...
# 8
> Is the same rule hold good for other primitive wrappers? or there is separate range for them?See reply #5
quittea at 2007-7-12 15:28:49 > top of Java-index,Java Essentials,Java Programming...