What is the Boolean.toString method in Java?

The toString method of the Boolean class can be used to get the Boolean value of a string.

Syntax

public String toString();
public String toString(boolean b);

Return value

If the argument is passed, then the string representation of the passed Boolean value is returned.

For `true` -- "true" is returned
For `false` -- "false" is returned

If the argument is not passed, then the string representation of the current Boolean object value is returned.

Code

class BooleanToString {
public static void main( String args[] ) {
Boolean trueVal = new Boolean(true);
System.out.println( "trueVal.toString() : " + trueVal.toString());
System.out.println( "trueVal.toString(true) : " + trueVal.toString(true));
System.out.println( "trueVal.toString(false) : " + trueVal.toString(false));
}
}

Explanation

In the code above:

  • We created a Boolean object with true as the value.

  • We called the toString() method without argument, and we will get the string representation of the object’s Boolean value. In our case, "true" will be returned.

  • We called the toString(true) method. For this, we will get the string representation of true; that is, "true" is returned. Then, we called toString(false). For this we will get "false" as a result.

Free Resources