DEV Community

Discussion on: Java Pitfalls: Unboxing null Booleans

Collapse
 
wldomiciano profile image
Wellington Domiciano

Nice! And if you are using Java 9+ and do not want to use third-party library, you can use java.utils.Objects#requireNonNullElse() method:

public boolean checkIfDelivered(Food food) {
  if(food == null) {
    return false;
  } else {
    return Objects.requireNonNullElse(food.wasDelivered(), false);
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
scottshipp profile image
scottshipp

Thanks! That’s a great suggestion!