Java.lang Package Classes

Java Boolean - valueOf() Method



The java.lang.Boolean.valueOf() method returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.

Syntax

public static Boolean valueOf(boolean b)

Parameters

b Specify a boolean value.

Return Value

Returns a Boolean instance representing b.

Exception

NA.

Example:

In the example below, the java.lang.Boolean.valueOf() method returns a Boolean instance representing the given boolean value.

import java.lang.*;

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

    //creating Boolean instance
    Boolean B1 = Boolean.valueOf(b1);
    Boolean B2 = Boolean.valueOf(b2);

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

    //printing the Boolean instance 
    System.out.println("The Boolean object B1 is: " + B1);
    System.out.println("The Boolean object B2 is: " + B2);   
  }
}

The output of the above code will be:

The boolean value b1 is: true
The boolean value b2 is: false
The Boolean object B1 is: true
The Boolean object B2 is: false

❮ Java.lang - Boolean