Java – Convert Boolean to String

You can convert an boolean value to string in Java using String class methods.

In this tutorial, we shall go through some of the ways to convert a boolean value to a string value, with example Java programs.

Boolean to String using Boolean.toString() method

toString() is a static method in Boolean class that returns string object representing the specified boolean value. We have to pass the boolean value as argument to toString() method.

In the following example we shall convert a boolean value (either true or false) to string by using Boolean.toString() function.

Java Program

/**
 * Java Program - Convert Boolean to String
 */

public class BooleanToString {

	public static void main(String[] args) {
		String str = Boolean.toString(true);
		System.out.println(str);
		
		str = (new StringBuffer()).append(false).toString();
		System.out.println(str);
	}
}

Output

true
false
ADVERTISEMENT

Boolean to String using String Concatenation

This is the most easiest way to convert an boolean value to a string. All you have to do is add an empty string to it.

In the following example we shall convert an boolean value to string by concatenating the number to an empty string.

Java Program

/**
 * Java Program - Convert Boolean to String
 */

public class BooleanToString {

	public static void main(String[] args) {
		boolean b = true;
		
		//convert boolean to string using string concatenation
		String str = b + "";
		System.out.println(str);
		
		//convert boolean false to string
		str = false + "";
		System.out.println(str);
	}
}

Run the above program and you shall get the following output.

Output

true
false

Boolean to String using String.valueOf() method

We can use String.valueOf() method to get a string value from a boolean value. Pass the boolean value as argument to String.valueOf() function, and the function returns a String object.

In the following example we shall convert a boolean value to string by using String.valueOf() function.

Java Program

/**
 * Java Program - Convert Boolean to String
 */

public class BooleanToString {

	public static void main(String[] args) {
		boolean b = true;
		
		//convert boolean to string using string concatenation
		String str = String.valueOf(b);
		System.out.println(str);
		
		//convert boolean false to string
		b = false;
		str = String.valueOf(b);
		System.out.println(str);
	}
}

Run this program and you shall get the following output.

true
false

Boolean to String using StringBuffer.append() method

Create a StringBuilder object and append the boolean value to it using append() method. append() method appends the string representation of the boolean value to this sequence. After append() function, call toString() method on the StringBuilder object, it returns string.

You can create StringBuilder object and call append() function and toString() function as chain in a single statement.

In the following example we shall convert a boolean to string by using StringBuilder class.

Java Program

/**
 * Java Program - Convert Boolean to String
 */

public class BooleanToString {

	public static void main(String[] args) {
		String str = (new StringBuffer()).append(true).toString();
		System.out.println(str);
		
		str = (new StringBuffer()).append(false).toString();
		System.out.println(str);
	}
}

Output

true
false

Conclusion

In this Java Tutorial, we learned how to convert or typecast a boolean to string in Java, using String concatenation or String.valueOf() method.