Read Also : Convert double to int in java
1. Convert String to boolean primitive using Boolean.parseBoolean() method
2. Convert String to Boolean object using Boolean.valueOf() method [Preferred]
3. Convert String to Boolean object using constructor of Boolean class [Not Recommended]
1. Convert String to boolean primitive : Using Boolean.parseBoolean(String str)
You can convert String to boolean primitive by using Boolean.parseBoolean(String str) method.According to Oracle docs,
The boolean returned by the method parseBoolean(String str) represents the value true if the string argument str is not null and is equal, ignoring case, to the string "true".
For example,
Boolean.parseBoolean("tRue") returns true
Boolean.parseBoolean("True") returns true
Boolean.parseBoolean("tRUE") returns true
Boolean.parseBoolean("yes") returns false
Boolean.parseBoolean("JavaHungry") returns false
public class StringtoBooleanPrimitive { public static void main(String args[]) { // Creating String Objects String str1 = "TruE"; String str2 = "hello";
/* Converting String Objects to boolean
using Boolean class parseBoolean() method*/
boolean bool1 = Boolean.parseBoolean(str1); boolean bool2 = Boolean.parseBoolean(str2);
// Printing the Boolean values System.out.println("Convert String to boolean primitive : " + bool1); System.out.println("Convert String to boolean primitive : " + bool2); } }
Output :
Convert String to boolean primitive : true
Convert String to boolean primitive : false
2. Convert String to Boolean Object : Using Boolean.valueOf(String str)
valueOf() returns Boolean instance representing the specified string.According to Oracle docs,
The Boolean object returned by the method valueOf(String str) represents the value true if the string argument str is not null and is equal, ignoring case, to the string "true".
For example,
Boolean.valueOf("tRue") returns true
Boolean.valueOf("True") returns true
Boolean.valueOf("tRUE") returns true
Boolean.valueOf("yes") returns false
Boolean.valueOf("JavaHungry") returns false
This method should be preferred over constructor Boolean(boolean) to convert String to Boolean object.Because this method yields significantly better space and time complexity.
public class StringtoBooleanObject { public static void main(String args[]) { // Creating String Objects String str1 = "TruE"; String str2 = "hello"; String str3 = "false"; /* Converting String Objects to Boolean Object
using Boolean class valueOf() method */
Boolean bool1 = Boolean.valueOf(str1); Boolean bool2 = Boolean.valueOf(str2); Boolean bool3 = Boolean.valueOf(str3); // Printing the Boolean values System.out.println("Convert String to Boolean Object : " + bool1); System.out.println("Convert String to Boolean Object : " + bool2); System.out.println("Convert String to Boolean Object : " + bool3); } }
Output :
Convert String to Boolean Object : true
Convert String to Boolean Object : false
Convert String to Boolean Object : false
3. Convert String to Boolean Object : Using Boolean Class Constructor
This method is not recommended to convert String to Boolean object. Joshua Bloch, author of the book Effective Java, himself recommended using static factory methods like valueOf(String str) as they offer caching which significantly improves space and time complexity.public class StringtoBooleanObject { public static void main(String args[]) { // Creating String Objects String str1 = "TruE"; String str2 = "false"; /* Converting String Objects to Boolean Object using Boolean Constructor */ Boolean bool1 = new Boolean(str1); Boolean bool2 = new Boolean(str2); // Printing the Boolean values System.out.println("Convert String to Boolean Object : " + bool1); System.out.println("Convert String to Boolean Object : " + bool2); } }
Output :
Convert String to Boolean Object : true
Convert String to Boolean Object : false
Note : Both valueOf(String str) and parseBoolean(String str) methods are present in the Boolean class
In the above article I have demonstrated 3 ways to convert String to Boolean primitive or Boolean Object in Java with Examples.If you have any questions regarding this topic then please mention in the comments.