9

I was wondering if there is a straightforward way (one-line, without creating a function) to convert String to Boolean in Java, but in a way that Boolean is null if String is null.

If I see it correctly, all of the methods from the Boolean class are returning false if the input string is null. Why is that?

Why is it better that Boolean.valueOf(String s) returns false in the case of s being null, rather than returning null?

1
  • 4
    Optional.ofNullable(s).map(Boolean::valueOf).orElse(null) is an alternative. Mainly useful if your value is already wrapped in an Optional. Commented Feb 25, 2015 at 9:48

3 Answers 3

7

Why is it better that Boolean.valueOf(String s) returns false in the case of s being null, rather than returning null?

Because out the build-in feature in Java, called autoboxing.

Let's suppose that Boolean.valueOf(String s) was actually returning null, when s is null. Then, the following statement would be throwing NullPointerException, because null cannot be assigned to a primitive:

boolean b = Boolean.valueOf(null);
Sign up to request clarification or add additional context in comments.

7 Comments

Nice point. But why does Integer.parseInt(null) throw NumberFormatException(null) - doesn't autoboxing also apply there?
@vikingsteve: It could be argued that a null String represents a false value for most uses. But what integer number should a null String represent? Also, as Peter correctly pointed out, Integer.parseInt already returns an int, not an Integer.
If you need a boolean from a String, then you'll use Boolean.parseBoolean, not valueOf. How could a misguided idiom, which goes through the redundant process of boxing followed by immediate unboxing, justify any API design decision?
On a wider note, most of the JDK API is designed around the fail-fast principle, forcing the client to make explicit null-checks everywhere instead of returning sensible defaults. The justification of the opposite in this one case would require much more elaboration.
@vikingsteve It would be in the spirit of the JDK to throw a NullPointerException and force the client to implement an explicit null-sentinel policy.
|
6

Why is it better that Boolean.valueOf(String s) returns false in the case of s being null, rather than returning null?

It is a matter of opinion, and (to be frank) pointless to ask ... unless you happen to be designing your own language and runtime libraries. The design decision was made more than 20 years ago, and there is zero chance that it will be changed while the language is called "Java".

The javadocs for Boolean.valueOf(String) don't offer any clues as to why the designers designed it this way.

However, it is clear that the behavior of Boolean.valueOf(String) is inconsistent with the behavior of valueOf(String) for the other primitive wrapper classes. For example Integer.valueOf(String) throws NumberFormatException if the string argument is null. So it is possible that the real explanation is that the Boolean.valueOf(String) semantics are just an unfortunate anomaly that is the result of an oversight that occurred in the rush to release Java 1.0.

However, that is speculation. For a real answer you would need to consult relevant internal Sun / Oracle documentary sources (if they still exist) and / or talk to someone on the original Java team (if they can still remember).

1 Comment

I have a favorite speculation for this: Boolean.getBoolean, that pinnacle of API perversion clearly dating from the earliest of early Java, actually has good reason to return false for a missing system property. Naturally, it relies internally on the API of Boolean.parseBoolean..
2

You can use the ternary operator :

String s = ...
Boolean b = s != null ? Boolean.valueOf(s) : null;

1 Comment

And then, the last part of the question, why is the actualy functionality of Boolean.valueOf() better than if it returned null?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.