There are two ways to convert a String to 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 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 work similar, it returns a true boolean value for a non-null String equal to true, ignoring 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.
// parseBoolean returns a boolean primitive value
String value = "true";
boolean b = Boolean.parseBoolean(value);
System.out.println(b);
Output
true
// 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 Boolean class e.g. new Boolean(String input) but I won't advise you to use that because everytime you will use this method it will return a new Boolean Object.
Instead, you should always use valueOf() method because Boolean instances are immutable and just two instances are enough to cover all scenarios.
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.
RUE 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 value e.g. both valueOf(true) and valueOf("true") will return same Boolean.TRUE object.
In short:
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 work similar, it returns a true boolean value for a non-null String equal to true, ignoring 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
// parseBoolean returns a boolean primitive value
String value = "true";
boolean b = Boolean.parseBoolean(value);
System.out.println(b);
Output
true
2. Boolean.valueOf() Examples
// 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 Boolean class e.g. new Boolean(String input) but I won't advise you to use that because everytime you will use this method it will return a new Boolean Object.
Instead, you should always use valueOf() method because Boolean instances are immutable and just two instances are enough to cover all scenarios.
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.
RUE 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 value e.g. both valueOf(true) and valueOf("true") will return same Boolean.TRUE object.
In short:
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 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 false boolean value instead of throwing NullPointerException.
4. Both methods are also case-insensitive, which means "true", "TRUE", and "True" will return 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.e. 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 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 provide better performance because it always returns the two pre-created instance i.e. Boolean.TRUE and Boolean.FALSE instead of creating a new object everytime you parse a String to boolean.