In Java, the toString() method of the Boolean class is used to convert a boolean value into its string representation. The Boolean class belongs to the java.lang package. The Boolean class provides both a static toString(boolean) method and an instance toString() method for obtaining the string representation of a boolean value.
- The returned string is either "true" or "false".
- The method is useful when a boolean value needs to be represented as text.
Syntax
public static String toString(boolean b)
- Parameter:
bis the boolean value to be converted. - Return Value: Returns "true" if the value is true, otherwise returns "false".
class Geeks
{
public static void main(String[] args)
{
// boolean type value
boolean value = true;
// static toString() method of Boolean class
String output = Boolean.toString(value);
// printing the value
System.out.println(output);
}
}
Output
true
Explanation: boolean value = true; creates a boolean variable with the value true. The Boolean.toString(value) method converts this boolean value into its string representation "true", which is stored in the output variable. Finally, System.out.println(output) prints the string true to the console
Example: Using Boolean.toString() method to convert false boolean value to its string representation.
class Geeks
{
public static void main(String[] args)
{
// boolean type value
boolean value = false;
// static toString() method of Boolean class
String output = Boolean.toString(value);
// printing the value
System.out.println(output);
}
}
Output
false
Explanation: boolean value = false; creates a boolean variable with the value false. The Boolean.toString(value) method converts the boolean value into the string "false" and stores it in the output variable. The System.out.println(output) statement then prints false to the console.