Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrapper doubt

 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which produces the output:
true
567 568
false

how the last output is coming false
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This statement y++; is converted too

y = Integer.valueOf(y.intValue() + 1);

So basically y is assigned a new reference to an Integer object. If you remember, all the wrapper classes are immutable i.e. the value in them cannot be changed...
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another interesting fact is that if you add y-- after y++ to make both values equal then also answer will be false.

Integer wrapper use only 8 bytes while comparing using == operator. Max allowed value of operand to be true is 127)
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepesh you are not right. The reason is the one that I gave. You can test it with this



Still the last output will be false.
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! I haven't looked at complete question because I found the clue at very first line My fact is also true. But primary resolution is given by you . Thanks for correction .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic