LAST UPDATED: DECEMBER 1, 2020
How to convert Java boolean to String
In Java, we can convert boolean
into a String in two ways either by using valueOf()
or toString()
method in Java. Let's see the examples.
1. By using String.valueOf()
method
The valueOf() method is a part of String Class. It is a static method that converts a boolean into a String value.
Example 1:
Here, a boolean value is passed and the value is converted into a String.
public class StudyTonight
{
public static void main(String args[])
{
boolean b1 = true;
boolean b2 = false;
String s1 = String.valueOf(b1);
String s2 = String.valueOf(b2);
System.out.println("The string value is : " +s1);
System.out.println("The string value is : " +s2);
}
}
The string value is : true
The string value is : false
2. By using Boolean.toString()
method
The toString()
method is a part of Boolean class. It is a static method that can also be used to convert boolean value to String.
Example 2:
Here, a boolean value is passed in the method is converted into a String.
public class StudyTonight
{
public static void main(String args[])
{
boolean b1 = true;
boolean b2 = false;
String s1 = Boolean.toString(b1);
String s2 = Boolean.toString(b2);
System.out.println("The string value is " +s1);
System.out.println("The string value is " +s2);
}
}
The string value is true
The string value is false