This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers and ==" Watch "Wrappers and ==" New topic
Author

Wrappers and ==

Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
in javaranch rules roundup the following givesanswer as false

Integer a = new Integer(5);
Integer b = new Integer(5);

what is the result of running
if(a==b)

Answer is false.....please explain why???

Whereas in Kathy &Bert book there isan example

Integer i1=10;
Integer i2=10;

then i1==i2 is true

Why are these different....from using new Integer???


SCJP 5.0<br />SCJD working on it
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

i'm guessing this is an old question from before the introduction of auto-boxing. the question was probably never updated.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
Fred,

I have tried running this code and it works the same way
so I dont think it needed updating.

To reiterate, my question,

when compiling and running the code below

*/
public class Primitive{

/** Creates a new instance of Class */


public static void main(String... args)
{
Integer a = 5;
Integer b = 5;
if(a==b) System.out.println("equal");
else System.out.println("notequal");

}
}

Prints equal

But when running the following code
public class Primitive{

/** Creates a new instance of Class */


public static void main(String... args)
{
Integer a = new Integer(5);
Integer b = new Integer(5);
if(a==b) System.out.println("equal");
else System.out.println("notequal");

}
}

Prints notequal

Please explain why....???

THanks in advance
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Both are correct.

Note that in the K&B example, the values are being autoboxed, and equal int values within the range of a byte will box to the same wrapper instance. But the Rules Roundup example is not using autoboxing -- it is creating separate objects using "new."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
kenny yen
Greenhorn

Joined: Oct 09, 2007
Posts: 7
it's because you created 2 new instance and they have got different hashcode. if you print the hashcode of both objects, you'll see they've got different values.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40

Originally posted by kenny yen:
it's because you created 2 new instance and they have got different hashcode. if you print the hashcode of both objects, you'll see they've got different values.


It actually has nothing to do with hashcode -- the first part was enough. There are 2 new (and different instances) and the == operator will report them as different, regardless of the hashcode.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrappers and ==
 
Similar Threads
question regarding auto-box/unbox
Dout on boxing
Wrappers question
error in roundup?
Material for Wrapper class