Java.lang Package Classes

Java Boolean - valueOf() Method



The java.lang.Boolean.valueOf() method returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

Syntax

public static Boolean valueOf(String s)

Parameters

s Specify a string.

Return Value

Returns the Boolean value represented by the string.

Exception

NA.

Example:

In the example below, the java.lang.Boolean.valueOf() method returns a Boolean object holding the boolean value given by the specified String.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating strings holding boolean values
    String s1 = "true";
    String s2 = "false";

    //creating Boolean instance
    Boolean b1 = Boolean.valueOf(s1);
    Boolean b2 = Boolean.valueOf(s2);

    //printing the string 
    System.out.println("The string s1 is: " + s1);
    System.out.println("The string s2 is: " + s2); 

    //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 string s1 is: true
The string s2 is: false
The Boolean object b1 is: true
The Boolean object b2 is: false

❮ Java.lang - Boolean