Utility method to convert Boolean into boolean and handle null in Java
Asked Answered
E

7

47

Is there a utility method in Java which converts Boolean into boolean and automatically handles null reference to Boolean as false?

Edrick answered 25/7, 2014 at 7:37 Comment(3)
Ermm ... it is one line of code ...Eth
possible duplicate of Booleans, conditional operators and autoboxingTropic
@StephenC Yeah ..but OP might searching for a smart line of code ;) (look at skeets solution)Irregularity
D
110

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.

Deal answered 25/7, 2014 at 7:40 Comment(0)
M
10

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);
}
Muddle answered 25/7, 2014 at 9:10 Comment(5)
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
2

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();
}
Irregularity answered 25/7, 2014 at 7:40 Comment(0)
D
2

Are you looking for a ready-made utility ? Then I think Commons-Lang BooleanUtils is the answer. It has a method toBoolean(Boolean bool).

Depict answered 25/7, 2014 at 7:41 Comment(0)
S
1

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
Selfesteem answered 24/3, 2021 at 12:22 Comment(0)
G
0

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();
}
Galley answered 25/7, 2014 at 7:40 Comment(2)
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
P
0

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'
}
Pantisocracy answered 24/6 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.