Java Convert boolean to String

Tags: String boolean

In this Java core tutorial we learn how to convert boolean value to String value in Java programming language.

Table of contents

  1. Convert boolean to String using String.valueOf() method
  2. Convert boolean to String using Boolean.toString() method

Convert boolean to String using String.valueOf() method

The first solution, we can use the String.valueOf() method to instantiate a String from boolean value as following Java program.

BooleanToStringExample1.java

public class BooleanToStringExample1 {
    public static void main(String... args) {
        boolean booleanValue1 = true;
        boolean booleanValue2 = false;

        String stringValue1 = String.valueOf(booleanValue1);
        String stringValue2 = String.valueOf(booleanValue2);

        System.out.println(stringValue1);
        System.out.println(stringValue2);
    }
}
The output as below.
true
false

Convert boolean to String using Boolean.toString() method

The second solution, we can use the Boolean.toString() method to convert a boolean value to a String as below Java code.

BooleanToStringExample2.java

public class BooleanToStringExample2 {
    public static void main(String... args) {
        boolean booleanValue1 = true;
        boolean booleanValue2 = false;

        String stringValue1 = Boolean.toString(booleanValue1);
        String stringValue2 = Boolean.toString(booleanValue2);

        System.out.println(stringValue1);
        System.out.println(stringValue2);
    }
}
The output as below.
true
false

Happy Coding 😊

Java Convert String to boolean

Java Convert boolean to int

Java Convert boolean to byte

Java Convert boolean to short

Java Convert boolean to long