aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes '==' operatot behaving different for 1000 and 10. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'==' operatot behaving different for 1000 and 10.

Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
I got confused in the following code where it shows (i3==i4) but Not the same with (i2==i4).
Why this code behaves differently. Here's the code

Output :Same Object-I



Kindly clear this doubt. I shall be very thankfull to u.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Austin,

If you create the integer like



It creates the integer object by auto boxing.When you create the Integer from -128 to 127 by using the above syntax, it creates the object in the constant pool like create the string in string literal pool.
If you create the Integer with the value more than 127,it creates the object in the heap.So at the time of check equality using == it returns false.

The above rule is applicable for Byte and Short also.But not applicable for Float and Double.



Cheers Munees
My Blog
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

No, that's not correct. There's no 'constant pool' for Integers. Autoboxing uses values cached by the Integer class.

The behavior is as mandated by the JLS.
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#190730
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.


luck, db
There are no new questions, but there may be new answers.
Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
Thanks Muneeswaran and Darryl But Darryl can you explain me the real cause of this absurd behaviour . Since my exam is next weak i need to know all possible answers .
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
Catherine austin wrote:Thanks Muneeswaran and Darryl But Darryl can you explain me the real cause of this absurd behaviour . Since my exam is next weak i need to know all possible answers .


His post has the real cause of the absurd behavior. JLS mandates that if you have a boxing of an int or short between -128 to 127 then when evaluating == it is true.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


yes that's correct, JVM returns same object for integer range -128 to 127. you may find my blogpost What is the problem while using "==" in autoboxing world in Java 5 interesting.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
Thanks Javin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: '==' operatot behaving different for 1000 and 10.