This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer Problem

 
Prashant Kataria
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i=10;
Integer j=10;

if(i==new Integer(10)){
System.out.print("i");
}
if(i==j){
System.out.print("j");
}

output should be "ij" but it only prints j why? if someone help me
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new Integer(10) newly creates an object on a heap. This is why 'i' is not printed out.

http://www.coderanch.com/t/259111/java-programmer-SCJP/certification/Wrapper-equality
[ September 17, 2006: Message edited by: Aniket Patil ]
 
Kidd He
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i==new Integer(10) creat a new Integer object,and "==" is used for checking the reference not the meaningful object's value.
Try equals(),and then it will do that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic