Java - Convert String to Boolean Example

There are two ways to convert a String to a boolean in Java, first, by using Boolean.parseBoolean() method, and second, by using Boolean.valueOf() method. The parseBoolean() method returns an equivalent boolean value of a given String, for example, if you pass "true" it will return the primitive boolean value true. Similarly, if you pass "false" it will return false. The good thing about this method is that it is case insensitive, which means if you pass "true", "TRUE", or "True" you will still get a true boolean value.

 Another good thing about this method is that it doesn't throw an exception if you pass any String value other than true and false. For example, if you pass "YES" it will return false, which is not obvious but that's still better than throwing an exception like NumberFormatException.

The Boolean.valueOf() method works similarly, it returns a true boolean value for a non-null String equal to true, ignoring the case, and returns false for everything else. It also returns a Boolean object instead of a primitive boolean value.

Also, both valueOf() and parseBoolean() methods are null safe, which means they will return false if you pass null i.e. parseBoolean(null) and valueOf(null) will return false instead of throwing NullPointerExcpeiton.

1. Boolean.parseBoolean() Examples

Here is a code example to convert a String to Boolean in Java using the Boolean.parseBoolean() method:
// parseBoolean returns a boolean primitive value
String value = "true";
boolean b = Boolean.parseBoolean(value);
System.out.println(b);

Output
true


2. Boolean.valueOf() Examples

Here is another code example to convert a String object to Boolean object using the valueOf() method of Boolean class from java.lang pacakge:
// valueOf returns a Boolean object
String data = "false";
boolean f = Boolean.valueOf(data);
System.out.println(f);
Output
false


Btw, there is another way to convert String to Boolean in Java, by using the constructor of the Boolean class e.g. new Boolean(String input) but I won't advise you to use that because every time you will use this method it will return a new Boolean Object.

Instead, you should always use the valueOf() method because Boolean instances are immutable and just two instances are enough to cover all scenarios.

Even Joshua Bloch has advised using prefer static factory method like valueOf() over constructor in his classic Effective Java book for performance and loose coupling. If you are interested, see Item 1 on Effective Java 3rd Edition to learn more about it.




Difference between parseBoolean and valueOf() in Java

Even though both methods can be used to parse a String to a boolean value, there is a slight difference between them. The parseBoolean() method returns a primitive boolean value while the valueOf() returns a Boolean object.

Though in the age of auto-boxing, a Boolean can easily be stored in a boolean variable, this is an important difference which you should remember.

Another benefit of using the valueOf() method is that it caches the boolean value and returns the already created Boolean.TRUE and Boolean.FALSE value instead of creating a new instance every time.

If you don't need a new instance of the Boolean object then you should always use Boolean.valueOf() method to create Boolean objects to get better performance.

This method is also overloaded to create a Boolean object from both Strings as well as primitive boolean values e.g. both valueOf(true) and valueOf("true") will return the same Boolean.TRUE object. See these Java Programming Courses for more details.

In short:
Java - Convert String to boolean



Java Program to Convert String to Boolean

Here is our complete Java program to convert String to Boolean in Java. It's quite similar to the earlier program for converting String to Integer, Long, Double, Float, Short, and Byte in Java. All of them follow the same technique to convert String to other data types. 

/**
 * 
 * A simple example to convert String to Boolean in Java
 */
public class Hello {

  public static void main(String args[]) {

    // parseBoolean returns a boolean primitive value
    String value = "true";
    boolean b = Boolean.parseBoolean(value);
    System.out.println(b);

    // valueOf returns a Boolean object
    String data = "false";
    boolean f = Boolean.valueOf(data);
    System.out.println(f);

    value = "NO";
    b = Boolean.parseBoolean(value);
    System.out.println(b);

    // null String will return false
    System.out.println(Boolean.parseBoolean(null));
    System.out.println(Boolean.valueOf(null));

    // any value other than true (Case-insensitive) will
    // return false
    System.out.println(Boolean.parseBoolean("YES"));
    System.out.println(Boolean.valueOf("Y"));
  }
}

Output
true
false
false
false
false
false
false

Important Points

Some important points about parseBoolean and valueOf methods which are worth remembering:

1. Both parseBoolean() and valueOf() are static methods defined in java.lang.Boolean class.

2. The parseBoolean() returns a primitive boolean value while valueOf() returns a Boolean object.

3. Both parseBoolean() and valueOf() are null-safe which means if you pass null String to them they will return a false boolean value instead of throwing NullPointerException.

4. Both methods are also case-insensitive, which means "true", "TRUE", and "True" will return the same Boolean.TRUE value.

5. Prefer valueOf() over parseBoolean() if you need Boolean object because it returns cached Boolean objects, defined in the Boolean class itself, I mean Boolean.TRUE for true and Boolean.FALSE for false.

6. The valueOf() provides better performance because of caching.

7. Autoboxing boolean primitive to Boolean object also uses the Boolean.valueOf() method.


That's all about how to convert a string to a boolean in Java. You should use the Boolean.parseBoolean() method if you need a primitive boolean value and Boolean.valueOf() if you need a Boolean object. 

The valueOf() method also provides better performance because it always returns the two pre-created instances like Boolean.TRUE and Boolean.FALSE instead of creating a new object every time you parse a String to boolean.


Further Learning

Thanks for reading this article. If you like this tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

Now, is the time for question, which method is better to convert a String to a Boolean object in Java? parseBoolean() or valueOf() and why? You can find the answer in the article as well, if you cannot answer try reading again.

1 comment:

  1. What is difference between boolean and Boolean in Java? I always think they are same because of auto-boxing

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.