• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Reference equality for Boolean reference value

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If == in java signifies object reference equality (instead of object value equality) .. why is it that:

Boolean x = true;
Boolean y = true;

System.out.println(x==y);

produce true?

Results is the same if both of them are false.

Could it be that in Java there are only 2 boolean object .. true and false. That's why x and y points to the same object?
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is there is some sort of automatic unwrapping happening here. Unboxing I think they call it? Just a guess though, I could be wrong.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Fred has it -- although it's wrapping ("boxing") that we're looking at.

When you write

Boolean x = true;

The Java compiler turns that into

Boolean x = Boolean.valueOf(true);

And the method Boolean.valueOf() will only every return the constants Boolean.TRUE or Boolean.FALSE. So every Boolean that you assign from the same literal value will compare as equal.

Something similar happens with other types: for example, the first 128 int values behave the same way, because Integer.valueOf(int) has a cache of the first 128 int values.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, ok I can see that Boolean x = true involves wrapping, now that you mention it, but what I meant was...
,
I had assumed that the x==y part involved unwrapping of the Boolean objects to primitives, before applying ==, as a way of explaining the way == was evaluated, which seems to be different than what is normal for objects in general. Get my drift?
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparison will only unbox if either variable has a primitive type:

boolean == boolean: no unboxing necessary, primitive comparison
boolean == Boolean or Boolean == boolean: unboxing of the Boolean, then primitive comparison
Boolean == Boolean: object reference comparison

The second one will throw a NullPointerException if the Boolean is null.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Comparison will only unbox if either variable has a primitive type:

boolean == boolean: no unboxing necessary, primitive comparison
boolean == Boolean or Boolean == boolean: unboxing of the Boolean, then primitive comparison
Boolean == Boolean: object reference comparison

The second one will throw a NullPointerException if the Boolean is null.



Understood, that answers my question clearly and succinctly. Thanks.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Even if that happens, then also x and y are two different references even if they refer to two diff objects having same content which is Boolean.TRUE ..
Isn't it ?

Thanks
Suren
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suren Singh wrote:



Even if that happens, then also x and y are two different references even if they refer to two diff objects having same content which is Boolean.TRUE ..
Isn't it ?

Thanks
Suren



The two different references both refer to the SAME object, Boolean.TRUE, not two different objects with the same content. So x and y are two different references to the same Object, which means the references hold the same value, which means they are equal in terms of ==.
 
Suren Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it thanks
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic