Is there recommended way of doing Boolean object c...
# android
d
Is there recommended way of doing Boolean object comparison in Kotlin? e.g.
if(Boolean.TRUE == someBooleanValue) { .. } else { .. }
, this example is using
Boolean.TRUE
from Java and android studio is giving lint warning:
a
if that's a bool, you shouldn't need the == true at all just if(someBool){}else{}
except in the nullability case, when I just use
somebool == true
d
I think
Boolean.TRUE
is more like object comparison. Does
somebool == true
do that?
z
Why do you want to compare boxes? If you have a Kotlin Boolean, it won't be boxed anyway unless it needs to be nullable.
a
exactly
d
Yes, it is a nullable Boolean.
z
Why do you want to compare specifically with
Boolean.TRUE
though? Kotlin will do the right thing if you just compare with
true
.
d
Sorry for inaccurate context. I was migrating some existing java code to kotlin, so it came up.
l
BTW, there's only 2 Boolean boxes (cached) since there's just 2 values. Just like all
Byte
values are cached on Android.
☝️ 1
d
These replies were helpful. Thank you guys!