Java boolean
allows values of true
and false
while Boolean allows true
, false
, and null
. I have started to convert my boolean
s to Boolean
s. This can cause crashes in tests such as
Boolean set = null;
...
if (set) ...
while the test
if (set != null && set) ...
seems contrived and error-prone.
When, if ever, is it useful to use Boolean
s with null values? If never, then what are the main advantages of the wrapped object?
UPDATE: There has been such a lot of valuable answers that I have summarised some of it in my own answer. I am at best an intermediate in Java so I have tried to show the things that I find useful. Note that the question is "incorrectly phrased" (Boolean cannot "have a null value") but I have left it in case others have the same misconception