Friday, August 19, 2011

'==' operator behaving different for 1000 and 10

class wrap
{
public static void main(String s[])
{

Integer i3=10;
Integer i4=10;
if(i3==i4)
System.out.println("Same Object-I");

Integer i1=1000;
Integer i2=1000;
if(i1==i2)
System.out.println("Same Object-II");

}}

Output :Same Object-I 

Reason: 
The Rule to be remembered is :
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.

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#190730 

No comments:

Post a Comment