Java – Convert String to Boolean

You can typecast or convert a String to Boolean in Java in many ways. Some of them are using Boolean.parseBoolean(), Boolean.valueOf(), new Boolean().

In this tutorial, we shall learn some of the ways of how to convert a string value to a boolean value with examples.

1. Convert string to boolean using Boolean.parseBoolean()

Boolean.parseBoolean(str) parses any parsable boolean value from string to boolean value.

In this example, we shall use Boolean.parseBoolean() method and pass some string values that could parse to a valid boolean value.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Boolean
 */

public class StringToBoolean {

	public static void main(String[] args) {
		
		boolean n;
		
		n = Boolean.parseBoolean("True"); //true
		System.out.println(n);
		
		n = Boolean.parseBoolean("TRUE"); //true
		System.out.println(n);
		
		n = Boolean.parseBoolean("true"); //true
		System.out.println(n);
		
		n = Boolean.parseBoolean("1235"); //false
		System.out.println(n);
		
		n = Boolean.parseBoolean("0"); //false
		System.out.println(n);
		
		n = Boolean.parseBoolean("false"); //false
		System.out.println(n);
		
		n = Boolean.parseBoolean("FALSE"); //false
		System.out.println(n);
		
		n = Boolean.parseBoolean("true "); //false
		System.out.println(n);
	}
}

Only the string "true" with case ignored is parsed as boolean true. Any other value parsed to Boolean.parseBoolean() returns false.

If you provide a null string value to parseBoolean(), the function returns false.

Run the above program and the String shall convert to Boolean, with results as shown in the following console output.

true
true
true
false
false
false
false
false
false

2. Convert string to boolean using Boolean.valueOf()

You can also use Boolean.valueOf() function to convert a string to boolean.

In the following example, we shall use the method valueOf() to get boolean value from string.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Boolean
 */

public class StringToBoolean {

	public static void main(String[] args) {
		
		boolean n;
		
		n = Boolean.valueOf("True"); //true
		System.out.println(n);
		
		n = Boolean.valueOf("TRUE"); //true
		System.out.println(n);
		
		n = Boolean.valueOf("true"); //true
		System.out.println(n);
		
		n = Boolean.valueOf("1235"); //false
		System.out.println(n);
		
		n = Boolean.valueOf("0"); //false
		System.out.println(n);
		
		n = Boolean.valueOf("false"); //false
		System.out.println(n);
		
		n = Boolean.valueOf("FALSE"); //false
		System.out.println(n);
		
		n = Boolean.valueOf("TR UE"); //false
		System.out.println(n);
		
		n = Boolean.valueOf(null); //false
		System.out.println(n);
	}
}

3. Convert string to boolean using Boolean() constructor

Note: new Boolean() constructor is depreciated. So, you may get warning when you are following this process. Boolean.valueOf() is recommended in the place of new Boolean().

You can also use the constructor of Boolean class, to convert a string to boolean.

In the following example, we shall use the constructor of Boolean class to convert from string to boolean.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Boolean
 */

public class StringToBoolean {

	public static void main(String[] args) {
		
		boolean n;
		
		n = new Boolean("True"); //true
		System.out.println(n);
		
		n = new Boolean("TRUE"); //true
		System.out.println(n);
		
		n = new Boolean("true"); //true
		System.out.println(n);
		
		n = new Boolean("1235"); //false
		System.out.println(n);
		
		n = new Boolean("0"); //false
		System.out.println(n);
		
		n = new Boolean("false"); //false
		System.out.println(n);
		
		n = new Boolean("FALSE"); //false
		System.out.println(n);
		
		n = new Boolean("TR UE"); //false
		System.out.println(n);
		
		n = new Boolean(null); //false
		System.out.println(n);
	}
}

Conclusion

In this Java Tutorial, we learned how to Convert a String to Boolean value in Java using Boolean.parseBoolean() and Boolean.valueOf() methods.