• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can anyone tell me the difference between these two program on wrapper class Integer  RSS feed

 
rojalin behera
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the difference between these two program on wrapper class Integer

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 == i2)
System.out.println("different objects");
if(i1.equals(i2))
System.out.println("meaningfully equal");

output: meaningfullyequal

Integer i1 = 10;
Integer i2 = 10;
if(i1 == i2)
System.out.println("different objects");
if(i1.equals(i2))
System.out.println("meaningfully equal");

output: different objects
meaningfully equal

Why these two output are different?

Thanks in Advance....
Rojalin

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rojalin behera wrote:Can anyone tell me the difference between these two program on wrapper class Integer

This looks like an almost carbon-copy of this question.

However, unless this is a question for the SCJP exam, I suggest you look at the AvoidTheEqualityOperator (←click) page.

Winston
 
rojalin behera
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got that ,I should avoid using == as I can do the same using equals().
But why these two output are different in the above posted two programs
Please explain me regarding this.

Thanks,
Rojalin
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rojalin behera wrote:if(i1 == i2)
System.out.println("different objects");


Hmmm ...
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your question, you need to understand that autoboxing makes use of the Integer.valueOf(int) method. Then take a look at the code for that method (you can find the JDK sources in src.zip in your installation folder).
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rojalin behera wrote:Please explain me regarding this.

Either follow the link that Winston posted or search these forums where this exact question has been asked (and answered) many times before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!