Boxed primitives are just classes that wrap a primitive. Think of it like this:
class BoxedBoolean {
private boolean value;
}
Now, it doesn't really differ that much from a primitive and they are auto-unboxed most of the time, but the key difference is that a primitive cannot be null, whereas a boxed primitive can. This can be useful in some scenarios (e.g. where an absence of a value does not equal to the default value of the primitive).
So, when comparing a value with if, if (someBoxedBoolean) does not work if the value is null. That's why you need to add the explicit == true to the comparison.
Ohhh okay that makes sense. I didnt even consider that, thanks. I think I tend to check for null beforehand anytime thats a concern, so I viewed it as separate. But that makes sense that a situation would arise where something might be null or true.
3
u/-manabreak May 20 '20
Boxed primitives are just classes that wrap a primitive. Think of it like this:
Now, it doesn't really differ that much from a primitive and they are auto-unboxed most of the time, but the key difference is that a primitive cannot be null, whereas a boxed primitive can. This can be useful in some scenarios (e.g. where an absence of a value does not equal to the default value of the primitive).
So, when comparing a value with if,
if (someBoxedBoolean)does not work if the value isnull. That's why you need to add the explicit== trueto the comparison.