• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers and ==

 
Mary John
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm guessing this is an old question from before the introduction of auto-boxing. the question was probably never updated.
 
Mary John
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
kenny yen
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic