How to Convert Boolean to String in Java
In this tutorial, we will learn how to convert a primitive data type boolean to a non primitive data type string in Java easily.
Sometimes, we need to convert values from one data type to another, under certain situation. Therefore, we will know two different ways to convert boolean to string in Java.
Two ways for converting boolean to string are as follows:
- Convert using String.valueOf(boolean) method of String class
- Convert using Boolean.toString(boolean) method of Boolean class
Let’s understand both ways one by one with example programs.
Convert boolean to String in Java using String.valueOf(boolean)
To convert primitive data type boolean to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts boolean to string. It is a static utility method of the String class.
Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:
public static String valueOf(boolean b)
This method accepts a boolean value that needs to be converted and returns the string representation of the boolean value.
Let’s create a simple program for converting a primitive boolean type to string in Java.
Example 1:
// Java Program to demonstrate the conversion of boolean into string using String.valueOf().
package stringPrograms;
public class BooleanToString {
public static void main(String[] args)
{
boolean b1 = true;
boolean b2 = false;
// Converting boolean to string using valueOf() method of String class.
String str1 = String.valueOf(b1);
String str2 = String.valueOf(b2);
System.out.println("String representation of boolean value: " +str1);
System.out.println("String representation of boolean value: " +str2);
}
}
Output: String representation of boolean value: true String representation of boolean value: false
Boolean to String Conversion using Boolean.toString() method
Boolean wrapper class in Java wraps a value of the primitive data type boolean in an object. An object of Boolean class contains a single field whose type is boolean.
The Boolean wrapper class also provides a toString() method that converts boolean value to String. The toString() method of Boolean class is a static utility method.
The general signature of this method is as below:
public static String toString(boolean b)
This method accepts a parameter b that is to be converted. It returns a string object representing the specified boolean value.
Let’s create a simple Java program for converting primitive type boolean to non primitive data type String using Boolean.toString() method.
Example 2:
// Java Program to demonstrate the conversion of boolean into string using Boolean.toString().
package stringPrograms;
public class BooleanToString {
public static void main(String[] args)
{
boolean b1 = true;
boolean b2 = false;
// Calling toString() method of Boolean class for converting boolean to string.
String str1 = Boolean.toString(b1);
String str2 = Boolean.toString(b2);
System.out.println("String representation of boolean value: " +str1);
System.out.println("String representation of boolean value: " +str2);
}
}
Output: String representation of boolean value: true String representation of boolean value: false
In this tutorial, you learned how to convert one data type boolean to another type String in Java. I hope that you will have understood the basic concept for converting boolean into string and practiced all programs.
Thanks for reading!!!