How to Convert String to boolean in Java
How to Convert String to boolean in Java
There are two methods to convert String to boolean:
- Using parseBoolean(string) method
- Using valueOf(string) method
If the string contains "True," "true," or "TRUE," it returns Boolean true. It returns Boolean false if the string doesn’t contain "True," “true," or "TRUE." It is case insensitive. Both the above methods are null safe; it means they will return false if you pass null. For example:
boolean b1=Boolean.parseBoolean(null);
Using parseBoolean(string)
It converts String into Boolean primitive. It is the static method of the Boolean wrapper class. 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. The signature of the method is given below:
public static int parseBoolean(String str)
Where str is the string to be converted into Boolean primitive.
Example
In the following example, we have taken two variables str1 and str2 of String type that contains "TRUE" and "true3" respectively. b1 and b2 are two variable that stores the primitive Boolean value of the string str1 and str2, respectively. Boolean is the wrapper class, and it invokes parseBoolean(string) method of the class. The method parses a string argument and converts it to Boolean primitive. The first println statement prints the Boolean value of the string str1, i.e., true and the second println statement prints the Boolean value of the string str2, i.e., false.
public class stringTobooleanExample { public static void main(String args[]) { String str1="TRUE"; String str2="true3"; boolean b1=Boolean.parseBoolean(str1); boolean b2=Boolean.parseBoolean(str2); System.out.println("The boolean value of str1: "+b1); System.out.println("The boolean value of str2: "+b2); } }
Output
The boolean value of str1: true The boolean value of str2: false
Using valueOf(string)
It also works similar to parseBoolean(string) method. The valueOf(string) method converts a string into the Boolean object. It returns the instance of the Boolean class instead of primitive Boolean value. It returns true for a non-null string and returns false for everything else. Boolean instances are immutable, so you should always use valueOf(string) method. The signature of the method is given below.
public static Boolean valueOf(String str)
Example
In the following example, we have taken two variables str1 and str2 of String type that contains "TRUE" and "" (null) respectively. b1 and b2 are two variable that stores the primitive Boolean value of the string str1 and str2 respectively. Boolean is the wrapper class and it invokes valueOf(string) method of the class. The method parses a string argument and converts it to Boolean primitive. The first println statement prints the Boolean value of the string str1 i.e. true and the second println statement prints the Boolean value of the string str2, i.e., false.
public class stringTobooleanExample1 { public static void main(String args[]) { String str1="TRUE"; String str2=""; //represents null boolean b1=Boolean.valueOf(str1); boolean b2=Boolean. valueOf (str2); System.out.println("Boolean value of str1: "+b1); System.out.println("Boolean value of str2: "+b2); } }
Output
Boolean value of str1: true Boolean value of str2: false