Java.lang.Boolean.parseBoolean() Method



The java.lang.Boolean.parseBoolean(String s) 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".


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

public static boolean parseBoolean(String s)


s − the String containing the boolean representation to be parsed

Return Value

This method returns the boolean represented by the string argument.




The following example shows the usage of lang.Boolean.parseBoolean() method.

package com.tutorialspoint;

import java.lang.*;

public class BooleanDemo {

   public static void main(String[] args) {

      // create and assign values to String's s1, s2
      String s1 = "TRue";
      String s2 = "yes";

      // create 2 boolean primitives bool1, bool2
      boolean bool1, bool2;

       *  static method is called using class name
       *  apply result of parseBoolean on s1, s2 to bool1, bool2
      bool1 = Boolean.parseBoolean(s1);
      bool2 = Boolean.parseBoolean(s2);

      String str1 = "Parse boolean on " + s1 + " gives "  + bool1;
      String str2 = "Parse boolean on " + s2 + " gives "  + bool2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );

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

Parse boolean on TRue gives true
Parse boolean on yes gives false