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.
Comments
-
Edd almost 2 years
Is there ever a situation where using
equals(Boolean)
and==
would return different results when dealing withBoolean
objects?Boolean.TRUE == myBoolean; Boolean.TRUE.equals(myBoolean);
I'm not thinking about primitive types here, just Boolean objects.
-
Menno about 11 yearsThough that would lead back to the primitive value.
-
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 about 11 yearsTo 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 about 11 yearswhy it print false? can you explain?
-
Marko Topolnik about 11 years
new Boolean(true) == Boolean.TRUE
would showcase the issue even better. -
John Dvorak about 11 years@MarkoTopolnik primitive values are a problem with tri-state booleans (true/false/null)
-
muneebShabbir about 11 yearsits false because we are comparing objects with
==
sign? -
Marko Topolnik about 11 years@Aquillo OP is only indicating that
myBoolean
is an object. -
John Dvorak about 11 years@MarkoTopolnik yeah... what's the shortest way to safely compare tri-state booleans?
-
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 istrue
. -
John Dvorak about 11 years@MarkoTopolnik only if the left hand side is a constant or two-state boolean
-
Marko Topolnik about 11 years@JanDvorak ...you mean, as in
TRUE.equals(myBoolean)
? :) -
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 about 11 years@JanDvorak Then you are in for a couple more characters of code :)
left == null? right == null : left.equals(right)
-
John Dvorak about 11 yearsIn ruby, neither of
TrueClass
,FalseClass
orNilClass
have a public constructor. -
assylias about 11 years@muneebShabbir The reason why it prints false is the same a for any other object: stackoverflow.com/questions/2772763/…
-
Edd about 11 yearsThanks 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 over 2 yearsExcept enums. You can use == for Enum objects because Java guaranteed it is singletone