44

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

ps-aux
  • 11,627
  • 25
  • 81
  • 128

6 Answers6

100

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

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);
}
Shailesh Aswal
  • 6,632
  • 1
  • 20
  • 27
  • 2
    These both allocate an unnecessary object. [Jon Skeet's answer](https://stackoverflow.com/a/24950467) doesn't. – Clement Cherlin Mar 24 '21 at 11:51
  • 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). – Shailesh Aswal Aug 06 '21 at 06:26
  • 1
    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? – Clement Cherlin Aug 10 '21 at 14:43
  • 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. – Shailesh Aswal Aug 11 '21 at 04:26
  • 1
    I have no problem with Optional when used as intended, a return type for methods which may or may not return a result. – Clement Cherlin Aug 11 '21 at 13:29
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();
}
Clement Cherlin
  • 387
  • 6
  • 13
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
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).

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
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
Clement Cherlin
  • 387
  • 6
  • 13
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();
}
Connorelsea
  • 2,308
  • 6
  • 26
  • 47