Boolean.TRUE == myBoolean vs. Boolean.TRUE.equals(myBoolean)

16,091

Solution 1

How about:

System.out.println(new Boolean(true) == new Boolean(true));
System.out.println(new Boolean(true) == Boolean.TRUE);

(both print false, for the same reason as any other type of objects).

Solution 2

if (Boolean.TRUE == new Boolean(true)) {
    System.out.println("==");
}

if (Boolean.TRUE.equals(myBoolean)) {
    System.out.println("equals");
}

In this case first one is false. Only second if condition is true.

It Prints:

equals

Solution 3

It would be dangerous to use == because myBoolean may not have originated from one of the constants, but have been constructed as new Boolean(boolValue), in which case == would always result in false. You can use just

myBoolean.booleanValue()

with neither == nor equals involved, giving reliable results. If you must cater for null-values as well, then there's nothing better than your equals approach.

Share:
16,091
Edd
Author by

Edd

I'm a java developer and I ain't afraid to show it

Updated on June 05, 2022

Comments

  • Edd
    Edd almost 2 years

    Is there ever a situation where using equals(Boolean) and == would return different results when dealing with Boolean objects?

    Boolean.TRUE == myBoolean;
    
    Boolean.TRUE.equals(myBoolean);
    

    I'm not thinking about primitive types here, just Boolean objects.

  • Menno
    Menno about 11 years
    Though that would lead back to the primitive value.
  • Marko Topolnik
    Marko Topolnik about 11 years
    @Aquillo You make it sound like that's a problem. Why would it be a problem? Note that the result of either of OP's expressions is the same primitive boolean.
  • Menno
    Menno about 11 years
    To me it would be a possible solution instead of a problem. Though OP stated his question is not to discuss the processing of primitives.
  • muneebShabbir
    muneebShabbir about 11 years
    why it print false? can you explain?
  • Marko Topolnik
    Marko Topolnik about 11 years
    new Boolean(true) == Boolean.TRUE would showcase the issue even better.
  • John Dvorak
    John Dvorak about 11 years
    @MarkoTopolnik primitive values are a problem with tri-state booleans (true/false/null)
  • muneebShabbir
    muneebShabbir about 11 years
    its false because we are comparing objects with == sign?
  • Marko Topolnik
    Marko Topolnik about 11 years
    @Aquillo OP is only indicating that myBoolean is an object.
  • John Dvorak
    John Dvorak about 11 years
    @MarkoTopolnik yeah... what's the shortest way to safely compare tri-state booleans?
  • Marko Topolnik
    Marko Topolnik about 11 years
    @JanDvorak No, you're right: TRUE.equals(myBoolean) is the shortest way---though not to compare tri-state booleans, but to determine if one such value is true.
  • John Dvorak
    John Dvorak about 11 years
    @MarkoTopolnik only if the left hand side is a constant or two-state boolean
  • Marko Topolnik
    Marko Topolnik about 11 years
    @JanDvorak ...you mean, as in TRUE.equals(myBoolean)? :)
  • John Dvorak
    John Dvorak about 11 years
    @MarkoTopolnik yeah, that would work. But, what if you had two tristates you wanted to check for equality?
  • Marko Topolnik
    Marko Topolnik about 11 years
    @JanDvorak Then you are in for a couple more characters of code :) left == null? right == null : left.equals(right)
  • John Dvorak
    John Dvorak about 11 years
    In ruby, neither of TrueClass, FalseClass or NilClass have a public constructor.
  • assylias
    assylias about 11 years
    @muneebShabbir The reason why it prints false is the same a for any other object: stackoverflow.com/questions/2772763/…
  • Edd
    Edd about 11 years
    Thanks for this... I wasn't sure if there are only ever 2 Boolean objects and all references point to those objects but I guess not
  • Ali Katkar
    Ali Katkar over 2 years
    Except enums. You can use == for Enum objects because Java guaranteed it is singletone