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

ps-aux picture ps-aux · Jul 25, 2014 · Viewed 20.1k times · Source

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

Answer

Jon Skeet picture Jon Skeet · Jul 25, 2014

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.