Is there a utility method in Java which converts Boolean
into boolean
and automatically handles null reference to Boolean
as false?
Utility method to convert Boolean into boolean and handle null in Java
Asked Answered
Ermm ... it is one line of code ... –
Eth
possible duplicate of Booleans, conditional operators and autoboxing –
Tropic
@StephenC Yeah ..but OP might searching for a smart line of code ;) (look at skeets solution) –
Irregularity
How about:
boolean x = Boolean.TRUE.equals(value);
? That's a single expression, which will only evaluate to true
if value
is non-null and a true-representing Boolean
reference.
On java 8 you can do:
static boolean getPrimitive(Boolean value) {
return Optional.ofNullable(value).orElse(false);
}
You can also do:
static boolean getPrimitive(Boolean value) {
return Boolean.parseBoolean("" + value);
}
These both allocate an unnecessary object. Jon Skeet's answer doesn't. –
Selfesteem
both are 'valid' and memory overheads are negligible.(The first one is simply an optional return, if its really so bad?) I am very well aware of 'jon skeets' answer, but this is more to demonstrate what is possible, (more so with OP's context of automatically handling null reference for Boolean). –
Muddle
One unnecessary object allocation per call is not negligible in code that is called, in a practical use case, once per boolean column, per row, per database fetch. Yes, the JVM might be able to optimize away the Optional via escape analysis. But it also might not. Why use a longer, less clear, allocating idiom when a shorter, clearer, non-allocating alternative exists? –
Selfesteem
with all due respect, please refer the OP, it didnt ask for most optimum way to do it, but the way in which null is handled automatically. Please take things in context. By your reasoning I suppose you are advocating not to use Optional? PS: The accepted answer is the best fit. –
Muddle
I have no problem with Optional when used as intended, a return type for methods which may or may not return a result. –
Selfesteem
I don't know whether it exists or not. I'd write a one liner like:
public static boolean getPrimitiveBoolean(Boolean bool) {
return bool == null ? false : bool.booleanValue();
}
Are you looking for a ready-made utility ? Then I think Commons-Lang BooleanUtils is the answer. It has a method toBoolean(Boolean bool).
If you're golfing, an explicit null check followed by automatic unboxing is shorter than the canonical answer.
boolean b=o!=null&&o; // For golfing purposes only, don't use in production code
This would be a method you could write that would do the trick. This would return false if the Boolean is null.
public static boolean toBooleanDefaultIfNull(Boolean bool) {
if (bool == null) return false;
return bool.booleanValue();
}
The return bool.booleanValue() ? true : false; is pretty useless, just do return bool.booleanValue() ;) –
Pagination
Skeet's answer is much better than this, but I'll change mine with your suggestion anyways. –
Galley
Alternative:
boolean isBooleanObjectTrue = booleanObject.booleanValue();
See usage:
public static void main(String[] args) {
Boolean booleanObject = Boolean.TRUE;
boolean isBooleanObjectTrue = booleanObject.booleanValue();
System.out.println(isBooleanObjectTrue); // prints: 'true'
booleanObject = Boolean.FALSE;
isBooleanObjectTrue = booleanObject.booleanValue();
System.out.println(isBooleanObjectTrue); // prints: 'false'
}
© 2022 - 2024 — McMap. All rights reserved.