• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Boolean unboxing.

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I have a question about situation like this:


I thought that here the == comparison will return false because == operator looks at the references and b3 is a primitive and b2 is an Object. But for some reason, the b2 got unboxed and the comparison resulted in the true value. Can anyone tell me if this can happen with some other types in Java? Or about some similar situations that may occur?

Cheers,
Jan.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boolean is a Wrapper class and wrapper classes are immutable
hence when there are more than two "true" values, they refer to the same literal or the same object

Can anyone tell me if this can happen with some other types in Java?


yes
• Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same

hope this helps
have a nice time
 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but this code:



return false. So I think it's not true that any two Booleans will return true with ==.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jan Osykowski wrote:but this code:

return false. So I think it's not true that any two Booleans will return true with ==.



== determines whether two refernce type are equal or not.
here you are creating a two different object with different refernce types.
So they will always return false,,

But

Will return True.check it out

Happy Learning





 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but I'm refering to my first post where I have a question about the == operator which is used on an object and primitive and returns true. I still don't get why...
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jan Osykowski wrote:Hey guys,

I have a question about situation like this:


I thought that here the == comparison will return false because == operator looks at the references and b3 is a primitive and b2 is an Object. But for some reason, the b2 got unboxed and the comparison resulted in the true value. Can anyone tell me if this can happen with some other types in Java? Or about some similar situations that may occur?

Cheers,
Jan.


The JLS on boolean equality operator:


If the operands of an equality operator are both of type boolean, or if one operand is of type boolean and the other is of type Boolean, then the operation is boolean equality. The boolean equality operators are associative.

If one of the operands is of type Boolean it is subjected to unboxing conversion (§5.1.8).

The result of == is true if the operands (after any required unboxing conversion) are both true or both false; otherwise, the result is false.

The result of != is false if the operands are both true or both false; otherwise, the result is true. Thus != behaves the same as ^ (§15.22.2) when applied to boolean operands.



So if you compare two Boolean objects, it will compare object identity, otherwise, it will compare boolean equality.
 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright! Thanks. Now it's all clear!
 
reply
    Bookmark Topic Watch Topic
  • New Topic