• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Class Integer equality(Programmer’s Guide to SCJP by Khalid A. Mughal,Review Question 10.10)

 
Rakesh kumar Gupta
Greenhorn
Posts: 14
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have solved many question on wrapper classes equality. I know that wrapper class is automatically unboxed where it needed.
But when i solve this question then i was confident about answer but this is so tricky.



My answer was
true
true
true
true

But when i show actual answer it was.
true
true
false
true

I was confused, i compile it and run and second answer was output.
Again i think it is near the max range of byte, then i change the value and show that
When value in Integer wrapper class is in the range of byte then '==' is same as equals() method means unbox occurs automatically
and when it is out of byte range then it behave like object which is created using new operator.
Now, I am unable to understand it?




 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh kumar Gupta wrote:Now, I am unable to understand it?

This topic has a very extensive discussion about wrapper classes, autoboxing and using the == operator. It's definitely worth reading. I'm pretty sure after carefully reading this topic all your doubts (concerning this topic) will be cleared. If you still have doubts/questions after reading this topic, simply click on the "Post reply" button and let us know

Hope it helps!
Kind regards,
Roel
 
Rakesh kumar Gupta
Greenhorn
Posts: 14
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.. Really a long explaination on wrapper.
I got it.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic