Java - Boolean parseBoolean(String s) method



Description

The Java Boolean parseBoolean(String) parses 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".

Declaration

Following is the declaration for java.lang.Boolean.parseBoolean() method

public static boolean parseBoolean(String s)

Parameters

s − the String containing the boolean representation to be parsed

Return Value

This method returns the boolean represented by the string argument.

Exception

NA

Example 1

The following example shows the usage of Boolean parseBoolean() method for a string value as true.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create and assign values to String s1
      String s1 = "TRue";

      // create 1 boolean primitive bool1
      boolean bool1;

      /**
       *  static method is called using class name
       *  apply result of parseBoolean on s1 to bool1
       */
      bool1 = Boolean.parseBoolean(s1);      
      String str1 = "Parse boolean on " + s1 + " gives "  + bool1;

      // print b1 value
      System.out.println( str1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Parse boolean on TRue gives true

Example 2

The following example shows the usage of Boolean parseBoolean() method for a a string value as Yes.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create and assign values to String s1
      String s1 = "Yes";

      // create 1 boolean primitive bool1
      boolean bool1;

      /**
       *  static method is called using class name
       *  apply result of parseBoolean on s1 to bool1
       */
      bool1 = Boolean.parseBoolean(s1);
      
      String str1 = "Parse boolean on " + s1 + " gives "  + bool1;

      // print b1 value
      System.out.println( str1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Parse boolean on Yes gives false

Example 3

The following example shows the usage of Boolean parseBoolean() method for a a string as null.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create a String variable s1
      String s1 = null;

      // create 1 boolean primitive bool1
      boolean bool1;

      /**
       *  static method is called using class name
       *  apply result of parseBoolean on s1 to bool1
       */
      bool1 = Boolean.parseBoolean(s1);
      
      String str1 = "Parse boolean on " + s1 + " gives "  + bool1;

      // print b1 value
      System.out.println( str1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Parse boolean on null gives false
java_lang_boolean.htm
Advertisements