Java.lang Package Classes

Java Boolean - toString() Method



The java.lang.Boolean.toString() method returns a String object representing the specified boolean. If the specified boolean is true, then the string "true" will be returned, otherwise the string "false" will be returned.

Syntax

public static String toString(boolean b)

Parameters

b Specify the boolean to be converted.

Return Value

Returns the string representation of the specified boolean.

Exception

NA.

Example:

In the example below, the java.lang.Boolean.toString() method returns a String object representing the given boolean.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating boolean value
    boolean b1 = true;
    boolean b2 = false;

    //printing the boolean value
    System.out.println("The b1 is: " + b1); 
    System.out.println("The b2 is: " + b2);

    //creating and printing the string 
    //representation of the boolean value
    System.out.println("The string value of b1 is: " + Boolean.toString(b1)); 
    System.out.println("The string value of b2 is: " + Boolean.toString(b2));    
  }
}

The output of the above code will be:

The b1 is: true
The b2 is: false
The string value of b1 is: true
The string value of b2 is: false

❮ Java.lang - Boolean