HOWTO · Java

How to Convert Boolean to String in Java

This tutorial teaches how to convert a boolean value to a string in Java. Explore various methods including String.valueOf(), Boolean.toString(), string concatenation, and String.format(). Learn how to effectively handle boolean to string conversions with clear examples and explanations.

On this page

In Java, the primitive boolean and the wrapper class Boolean both represent true or false. For a primitive, use String.valueOf(boolean) or the static Boolean.toString(boolean) overload. For a non-null Boolean object, its instance toString() method works as well. Concatenation and formatting are useful when the value belongs in a larger message. These conversions produce lowercase text, so true becomes "true" and false becomes "false".

Convert a primitive boolean to a string with String.valueOf(boolean)

String.valueOf(boolean) is a static method on String that accepts a primitive boolean and returns its text representation. It is a direct choice when the value is already primitive, and the result can be stored, compared, or passed to a method that expects a String. The input remains a boolean; the returned value is text, so it is not suitable for later boolean conditions without converting it back.

Run this example:

public class BooleanStringExample {
    public static void main(String[] args) {
        boolean active = true;
        String text = String.valueOf(active);
        System.out.println(text);
    }
}
true

This form also works with false and returns the corresponding lowercase string. Use the primitive overload when the source variable is declared as boolean; Java can also unbox a non-null Boolean when a primitive argument is required. If a wrapper reference may be null, handle that possibility before passing it to a method that expects a primitive, because unboxing null causes a NullPointerException.

Convert a Boolean value to a string with Boolean.toString()

Boolean is the wrapper class, while boolean is the primitive type. A non-null Boolean object can call the instance method toString(). The class also provides the static Boolean.toString(boolean) overload, which accepts a primitive directly and avoids creating a wrapper just to convert the value. The example shows both forms so you can choose based on the type you already have.

public class BooleanStringExample {
    public static void main(String[] args) {
        Boolean enabled = Boolean.FALSE;
        String instanceText = enabled.toString();
        boolean active = true;
        String staticText = Boolean.toString(active);
        System.out.println("Instance: " + instanceText);
        System.out.println("Static: " + staticText);
    }
}
Instance: false
Static: true

The instance call requires a non-null reference: if enabled is null, enabled.toString() throws NullPointerException. The static overload is called on the class and takes a primitive value, so it does not dereference a wrapper. If you receive a nullable Boolean, decide what null should mean before calling the instance method or unboxing it. For example, you can branch on the reference and supply a fallback string when it is null.

Convert a boolean or Boolean value to a string by concatenation

When a string is already being built, Java converts a primitive boolean to its lowercase text during concatenation. A non-null Boolean wrapper is also converted to its corresponding text. Put the string first to make a label easy to read, such as "Enabled: " + enabled. This approach is convenient for messages and logs; for a standalone conversion, an explicit method communicates the intent more clearly.

public class BooleanStringExample {
    public static void main(String[] args) {
        boolean active = true;
        Boolean enabled = Boolean.FALSE;
        System.out.println("Active: " + active);
        System.out.println("Enabled: " + enabled);
    }
}
Active: true
Enabled: false

Concatenating a null Boolean produces the text "null"; it does not call toString() on that reference. This differs from enabled.toString(), which throws NullPointerException for the same null reference. Keep the distinction in mind when building output: concatenation preserves null as the word null, while the instance method requires a real Boolean value. If the text is intended for a user or an external format, choose an explicit fallback when null would be ambiguous.

Format a boolean as a string with String.format()

Use String.format() when the boolean belongs in a larger formatted message or when the message contains several values. Its %b conversion formats a Boolean argument as lowercase true or false; it is useful for readable templates, though it adds formatting machinery for a single value. This example keeps the format string and value together, making the position of the boolean clear.

public class BooleanStringExample {
    public static void main(String[] args) {
        boolean available = true;
        String message = String.format("Available: %b", available);
        System.out.println(message);
    }
}
Available: true

For a simple conversion, prefer String.valueOf(boolean) or Boolean.toString(boolean). Use concatenation for a short label and String.format() when a template improves the structure of a longer message. In all cases, keep the source type in mind: a primitive always has a boolean value, while a Boolean reference can be null.