Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String to Boolean in Java, but to treat null differently than false?

Tags:

java

boolean

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?

like image 955
Branex Avatar asked Feb 25 '15 09:02

Branex


People also ask

Can we convert string to boolean in Java?

To convert String to boolean in Java, you can use Boolean. parseBoolean(string). But if you want to convert String to Boolean object then use the method Boolean. valueOf(string) method.

Can we assign null value to boolean in Java?

Nullable boolean can be null, or having a value “true” or “false”. Before accessing the value, we should verify if the variable is null or not. This can be done with the classical check : if … else …

How do you convert string false to boolean false?

The easiest way to convert string to boolean is to compare the string with 'true' : let myBool = (myString === 'true'); For a more case insensitive approach, try: let myBool = (myString.


3 Answers

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);
like image 103
Konstantin Yovkov Avatar answered Oct 05 '22 18:10

Konstantin Yovkov


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).

like image 34
Stephen C Avatar answered Oct 05 '22 18:10

Stephen C


You can use the ternary operator :

String s = ...
Boolean b = s != null ? Boolean.valueOf(s) : null;
like image 40
Eran Avatar answered Oct 05 '22 19:10

Eran