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 Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== doubt

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i=new Integer('1');
Character cc=new Character('1');
if(i.equals(cc)){
System.out.println("equals");
}
if(i.intValue()==cc.charValue()){
System.out.println("EQ");
}

this code prints EQ.but why is i.intValue()==cc.charValue() equal?
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xxxValue returns the primitive value of a Wrapper object, so your code essentially does this:



Their primitive values are equal.
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
 
John Lincoln
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Also byte,short,char are int compatible.

If you see there is no constructor in Integer class which takes char as argument. So '1' is internaly converted to and it creates an Integer object.

let me know if my understanding is correct

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic