• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Wrappers question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second question is


Why i1 == i3 returns false?
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what happens when I compare b2 == b3, boxing or unboxing occurs?

Thanks.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifications!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic