Convert boolean to String in Java
In Java, a primitive boolean value can be converted to a String containing "true" or "false". The clearest choices are Boolean.toString(boolean) and String.valueOf(boolean).
For example, converting true produces the string "true", while converting false produces "false". This is a value conversion, not a Java type cast.
Java boolean-to-String syntax
The two direct forms are:
String str1 = Boolean.toString(booleanValue);
String str2 = String.valueOf(booleanValue);
Both return the lowercase strings "true" and "false".
Convert boolean to String using Boolean.toString()
Boolean.toString(boolean) is a static method of the Boolean class. Pass the primitive boolean value as the argument, and the method returns its string representation.
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
The first statement directly demonstrates Boolean.toString(true). It returns a String, so the value stored in str is "true".
Convert boolean to String using String.valueOf()
String.valueOf(boolean) is another direct way to convert a primitive boolean to a string. Pass the boolean value to the method and it returns "true" or "false".
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
For ordinary boolean-to-string conversion, String.valueOf() and Boolean.toString() produce the same text for primitive boolean values.
Convert boolean to String using String concatenation
A boolean can also become part of a string through concatenation. When a boolean is concatenated with an empty string, Java converts the boolean to its textual representation.
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
Although b + "" works, Boolean.toString(b) or String.valueOf(b) usually expresses the conversion more clearly when conversion is the only goal. Concatenation is more natural when the boolean is already being included in a larger message.
boolean active = true;
String message = "Active: " + active;
System.out.println(message);
Active: true
Convert boolean to String using StringBuffer.append()
A boolean can also be appended to a StringBuffer. The append(boolean) method appends the string representation of the boolean value, and toString() then returns the complete sequence as a String.
This approach is mainly useful when you are already building a string from several values. For converting one boolean value alone, Boolean.toString() or String.valueOf() is simpler.
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
Convert Java Boolean object to String, including null
Java also has the wrapper type Boolean. Unlike the primitive boolean, a Boolean reference can be null. This matters when choosing a conversion method.
public class BooleanObjectToString {
public static void main(String[] args) {
Boolean first = Boolean.TRUE;
Boolean second = null;
System.out.println(String.valueOf(first));
System.out.println(String.valueOf(second));
}
}
true
null
When a Boolean object is supplied to String.valueOf(Object), a null reference is represented by the string "null". In contrast, calling an instance method such as value.toString() on a null Boolean reference causes a NullPointerException.
Convert Java boolean to “1” or “0” instead of “true” or “false”
The standard boolean-to-string methods return "true" or "false"; they do not return "1" or "0". If an application requires numeric text, define that mapping explicitly with the conditional operator.
boolean enabled = true;
String value = enabled ? "1" : "0";
System.out.println(value);
1
For false, the same expression returns "0". This is a custom representation and is different from Java’s normal boolean string representation.
Boolean.toString() vs String.valueOf() for Java boolean conversion
| Approach | Primitive boolean result | Best suited for |
|---|---|---|
Boolean.toString(b) | "true" or "false" | Direct, explicit boolean conversion |
String.valueOf(b) | "true" or "false" | General-purpose value-to-string conversion |
b + "" | "true" or "false" | Works, but is less explicit for standalone conversion |
StringBuffer.append(b).toString() | "true" or "false" | Building a string from several pieces |
b ? "1" : "0" | "1" or "0" | Custom numeric text representation |
Which Java boolean-to-String method should you use?
Use Boolean.toString(boolean) or String.valueOf(boolean) when you simply need "true" or "false". Both are direct and readable. Use concatenation when the boolean is naturally part of a larger string, and use StringBuffer or a similar string-building class when you are already assembling several values.
If the required output is "1" or "0", use an explicit conditional expression because Java’s standard boolean string conversion produces "true" and "false".
In this Java Tutorial, we learned how to convert a boolean to string in Java using Boolean.toString(), String.valueOf(), string concatenation, and StringBuffer.append(), and how to create custom "1"/"0" string values when required.
TutorialKart.com