Java.lang Package Classes

Java Boolean - parseBoolean() Method



The java.lang.Boolean.parseBoolean() method is used to parse the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Syntax

public static boolean parseBoolean(String s)

Parameters

s Specify the String containing the boolean representation to be parsed.

Return Value

Returns the boolean represented by the string argument.

Exception

NA.

Example:

In the example below, the java.lang.Boolean.parseBoolean() method is used to parse the given string argument as a boolean.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating string arguments
    String s1 = "TRUE";
    String s2 = "False";
    String s3 = "ABC";

    //creating Boolean instance
    boolean b1 = Boolean.parseBoolean(s1);
    boolean b2 = Boolean.parseBoolean(s2);
    boolean b3 = Boolean.parseBoolean(s3);

    //printing the boolean value 
    System.out.println("parsing s1 creates: " + b1);
    System.out.println("parsing s2 creates: " + b2);
    System.out.println("parsing s3 creates: " + b3);  
  }
}

The output of the above code will be:

parsing s1 creates: true
parsing s2 creates: false
parsing s3 creates: false

❮ Java.lang - Boolean