Java – String to boolean conversion in 3 ways

In this article, we will discuss various ways to convert String to Boolean in Java

Q) What is the need of converting String to primitive boolean or Boolean wrapper-type ?

  • Generally, whenever we receive any data from web application then it is passed in the form of String only
  • To use Boolean flags in Java application, string with either true or false values needs to be converted into Boolean first and then we can use for any logical expression evaluation
  • This article explains about String to primitive boolean or Boolean wrapper-type conversion only, but we can do conversion for other types like int, double, float, long, etc
  • Note: Likewise, sometime Boolean to String conversion is also required
  • Read String class in detail with example

1. Various ways to convert String to Boolean

  1. Using Boolean.parseBoolean(“strValue”);
  2. Using Boolean.valueOf(“strValue”);
  3. Create Boolean object and then invoke booleanValue() method {i.e.; new Boolean(“strValue”).booleanValue();}

Let us move forward and discuss all possible ways to convert String to Boolean in Java

1.1 Using Boolean.parseBoolean(“strValue”); method

  • This method can be used to convert String into primitive boolean data-type
  • Only for string consisting true value, returns boolean true
  • For all other string value, returns boolean false
  • Note: true can be in upper-case or lower-case or combination of both

Method signature:

public static boolean parseBoolean(String s);

ConvertStringIntoBooleanUsingParseBooleanMethod.java

package in.bench.resources.string.to.bool.conversion;

public class ConvertStringIntoBooleanUsingParseBooleanMethod {

	public static void main(String[] args) {

		// String (either true or false)
		String str1 = "true";

		// 1. converting String to boolean
		boolean boolValue1 = Boolean.parseBoolean(str1);
		System.out.println("1. Converted boolean value is : "
				+ boolValue1);

		// String (either true or false)
		String str2 = "false";

		// 2. converting String to boolean
		boolean boolValue2 = Boolean.parseBoolean(str2);
		System.out.println("2. Converted boolean value is : "
				+ boolValue2);

		// String (any string value)
		String str3 = "abcd";

		// 2. converting String to boolean
		boolean boolValue3 = Boolean.parseBoolean(str3);
		System.out.println("3. Converted boolean value is : "
				+ boolValue3);
	}
}

Output:

1. Converted boolean value is : true
2. Converted boolean value is : false
3. Converted boolean value is : false

1.2 Using Boolean.valueOf(“strValue”); method

  • This method can be used to convert String into primitive boolean data-type
  • Only for string consisting true value, returns Boolean true
  • For all other string value, returns Boolean false
  • Note: true can be in upper-case or lower-case or combination of both

Method signature:

public static Boolean valueOf(String s);

ConvertStringIntoBooleanUsingValueOfMethod.java

package in.bench.resources.string.to.bool.conversion;

public class ConvertStringIntoBooleanUsingValueOfMethod {

	public static void main(String[] args) {

		// String (either true or false)
		String str1 = "True";

		// 1. converting String to Boolean
		Boolean boolValue1 = Boolean.valueOf(str1);
		System.out.println("1. Converted Boolean value is : "
				+ boolValue1);

		// String (either true or false)
		String str2 = "False";

		// 2. converting String to Boolean
		Boolean boolValue2 = Boolean.valueOf(str2);
		System.out.println("\n2. Converted Boolean value is : "
				+ boolValue2);

		// String (any string value)
		String str3 = "abcd";

		// 3. converting String to Boolean
		Boolean boolValue3 = Boolean.valueOf(str3);
		System.out.println("\n3. Converted Boolean value is : "
				+ boolValue3);
	}
}

Output:

1. Converted Boolean value is : true

2. Converted Boolean value is : false

3. Converted Boolean value is : false

1.3 Create Boolean object and then invoke booleanValue() method

  • Here, we will create new Boolean object with string as constructor-argument
  • After creating new Boolean object by passing string-value, then invoke booleanValue() method for converting String to primitive boolean data-type
  • Only for string consisting true value, returns boolean true
  • For all other string value, returns boolean false
  • Note: true can be in upper-case or lower-case or combination of both

Method signature:

public Boolean(String s);

public boolean booleanValue();

ConvertStringIntoBooleanUsingBooleanValueMethod.java

package in.bench.resources.string.to.bool.conversion;

public class ConvertStringIntoBooleanUsingBooleanValueMethod {

	public static void main(String[] args) {

		// String (either true or false)
		String str1 = "TRUE";

		// 1. converting String to boolean
		// by creating new Boolean Object
		Boolean bool1 = new Boolean(str1);
		boolean boolValue1 = bool1.booleanValue();
		System.out.println("1. Converted boolean value is : "
				+ boolValue1);

		// String (either true or false)
		String str2 = "false";

		// 2. converting String to boolean
		// by creating new Boolean Object
		Boolean bool2 = new Boolean(str2);
		boolean boolValue2 = bool2.booleanValue();
		System.out.println("\n2. Converted boolean value is : "
				+ boolValue2);

		// String (any string value)
		String str3 = "abcd";

		// 3. converting String to boolean
		// by creating new Boolean Object
		Boolean bool3 = new Boolean(str3);
		boolean boolValue3 = bool3.booleanValue();
		System.out.println("\n3. Converted boolean value is : "
				+ boolValue3);
	}
}

Output:

1. Converted boolean value is : true

2. Converted boolean value is : false

3. Converted boolean value is : false

Q) What if we want to convert primitive boolean data-type to Boolean wrapper-type or vice-versa ?

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

2. Auto-boxing and un-boxing feature from Java 1.5 version:

  • parseBoolean() method returns primitive boolean type but it can be easily used as Boolean wrapper-type
  • as auto-boxing feature helps to convert primitive type to respective wrapper-types
  • similarly, Boolean.valueOf() method returns Boolean wrapper-type but it can also be used as primitive boolean data-type
  • let’s see one example on this auto-boxing feature at the very end

AutoBoxingFeatureForBooleanConversion.java

package in.bench.resources.string.to.bool.conversion;

public class AutoBoxingFeatureForBooleanConversion {

	public static void main(String[] args) {

		// String (either true or false)
		String str1 = "true";

		// converting String to boolean
		boolean boolValue1 = Boolean.parseBoolean(str1);

		// 1. Auto-Boxing - converting boolean to Boolean
		Boolean boolAutoBoxing = boolValue1;
		System.out.println("1. Auto-Boxing : "
				+ boolAutoBoxing);

		// String (either true or false)
		String str2 = "False";

		// converting String to Boolean
		Boolean boolValue2 = Boolean.valueOf(str2);

		// 2. Un-Boxing - converting Boolean to boolean
		boolean boolUnBoxing = boolValue2;
		System.out.println("\n2. Un-Boxing   : "
				+ boolUnBoxing);
	}
}

Output:

1. Auto-Boxing : true

2. Un-Boxing   : false

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Boolean to String conversion in 6 ways
Java - Long to String conversion in 6 ways