Java Convert boolean to String
In this Java core tutorial we learn how to convert boolean value to String value in Java programming language.
Table of contents
- Convert boolean to String using String.valueOf() method
- 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);
}
}
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);
}
}
true
false
Happy Coding 😊