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

Wrappers question

 
Natallia Bahlai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why b2 == b3 results true while b1 == b2 return false?

Thanks in advance.
 
Natallia Bahlai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second question is


Why i1 == i3 returns false?
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Result :
false
true
true


b1==b2 results false because it checks the same object in memory, especially checks the same object's id in memory..
 
Natallia Bahlai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens when I compare b2 == b3, boxing or unboxing occurs?

Thanks.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b2 will unbox from Boolean to boolean, and compare unboxed value with value of b3
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natallia Bahlai wrote:And what happens when I compare b2 == b3, boxing or unboxing occurs?
.


i think it is done implicitly by compiler, because either boxing or unboxing occurs will give the same result...

Result : true


Result : true
 
Purvesh Maheshwari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natallia Bahlai wrote:The second question is

Why i1 == i3 returns false?




When AUTO-Boxing done at line Integer i3=i2,new object implicitly generated and the int value is assigned to new object.when you trying to compare two different object using == then it will return false.

Also AUTO-Unboxing will be done while comparing one primitive type with object of respective wrapper class.
But NO AUTO (UNBOXING OR BOXING) operation happen while comparing two object using == operate.

Hope this will help you Natallia.
 
Natallia Bahlai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifications!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic