Java – Boolean to String conversion in 6 ways

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

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

  • Generally, whenever we receive or send data from web application then it is passed in the form of String only
  • After logical expression evaluation, results are stored in Boolean flags which needs to be converted into String for displaying purpose
  • This article explains about primitive boolean or Boolean wrapper-type to String conversion only, but we can do conversion for other types like int, double, float, long, etc. to String object
  • Note: Likewise, sometime String to Boolean conversion is also required

1. Various ways to convert Boolean to String:

  1. Using Boolean.toString(boolValue);
  2. Using String.valueOf(boolValue);
  3. Using String.format(type, boolValue);
  4. Create Boolean object and then convert to String using toString() method {new Boolean(boolValue).toString();}
  5. Adding double quotes (“”) to boolean value {i.e.; “” + boolValue;}
  6. Using append() method of StringBuffer & StringBuilder

Read String class in detail with example

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

1.1 Using Boolean.toString(boolValue); method

  • This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object

Method signature:

public static String toString(boolean b);

ConvertBooleanIntoStringUsingToStringMethod.java

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

public class ConvertBooleanIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;

		// 1. converting boolean to String
		// by using toString() method
		String str1 = Boolean.toString(boolValue1);
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is : "
				+ str1);

		// primitive boolean data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting boolean to String
		// by using toString() method
		String str2 = Boolean.toString(boolValue2);
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(true);

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

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : true

1.2 Using String.valueOf(boolValue); method

  • This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object

Method signature:

public static Boolean valueOf(boolean b);

ConvertBooleanIntoStringUsingValueOfMethod.java

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

public class ConvertBooleanIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;

		// 1. converting long to String
		// by using valueOf() method
		String str1 = String.valueOf(boolValue1);
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is : "
				+ str1);

		// primitive boolean data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting boolean to String
		// by using valueOf() method
		String str2 = String.valueOf(boolValue2);
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(false);

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

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : false

1.3 Using String.format(type, number); method

  • This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object

Method signature:

public static String format(String format, Object... args);

ConvertBooleanIntoStringUsingFormatMethod.java

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

public class ConvertBooleanIntoStringUsingFormatMethod {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;

		// 1. converting boolean to String
		// by using format() method
		String str1 = String.format("%b", boolValue1);
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting negative long to String
		// by using format() method
		String str2 = String.format("%b", boolValue2);
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(true);

		// 3. converting Boolean object to String
		// by using format() method
		String str3 = String.format("%b", boolValue3);
		System.out.println("\n3. Converted"
				+ " Boolean object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : true

1.4 Create Boolean object and then convert to String using toString() method

  • Here, we will create new Boolean object with primitive bolean value as constructor-argument
  • After creating new Boolean object by passing primitive boolean value, then invoke toString() method for converting primitive boolean data-type to String object

Method signature:

public Boolean(boolean value);

public String toString();

ConvertBooleanIntoStringUsingObjectCreation.java

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

public class ConvertBooleanIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;

		// 1. converting boolean to String
		// by creating Long object
		Boolean boolObj1 = new Boolean(boolValue1);

		// and then invoke toString(); method
		String str1 = boolObj1.toString();
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting negative long to String
		// by creating Long object
		Boolean boolObj2 = new Boolean(boolValue2);

		// and then invoke toString(); method
		String str2 = boolObj2.toString();
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(false);

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

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : false

1.5 Adding double quotes (“”) to Boolean flag-value

  • Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + boolValue;

ConvertBooleanIntoStringByAddingDoubleQuotes.java

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

public class ConvertBooleanIntoStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;;

		// 1. converting boolean to String by adding ""
		String str1 = "" + boolValue1;
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is :"
				+ str1);

		// primitive boolean data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting negative boolean to String by adding ""
		String str2 = "" + boolValue2;
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(true);

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

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : true

1.6 Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive boolean data-type to String object by appending to StringBuffer or StringBuilder object
  • And then invoking toString() method

Syntax:

StringBuffer sb = sb.append(boolValue);

String temp = sb.toString();

ConvertBooleanIntoStringByAppending.java

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

public class ConvertBooleanIntoStringByAppending {

	public static void main(String[] args) {

		// primitive boolean data-type
		boolean boolValue1 = true;

		// 1. converting boolean to String
		// by using append() method
		StringBuffer sb1 = new StringBuffer();
		String str1 = sb1.append(boolValue1).toString();
		System.out.println("1. Converted"
				+ " primitive boolean TRUE to String value is : "
				+ str1);

		// primitive boolean data-type (negative value)
		boolean boolValue2 = false;

		// 2. converting boolean to String
		// by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(boolValue2).toString();
		System.out.println("\n2. Converted"
				+ " primitive boolean FALSE to String value is : "
				+ str2);

		// Boolean object
		Boolean boolValue3 = new Boolean(false);

		// 3. converting Boolean object to String
		// by using append() method
		StringBuffer sb3 = new StringBuffer();
		String str3 = sb3.append(boolValue3).toString();
		System.out.println("\n3. Converted"
				+ " Boolean object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive boolean TRUE to String value is : true

2. Converted primitive boolean FALSE to String value is : false

3. Converted Boolean object to String value is : false

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

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

Q) What if we want to convert to Boolean wrapper-type ?

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

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 - String to char conversion in 2 ways
Java - String to boolean conversion in 3 ways