Skip to main content

Convert String Object to Boolean in java

Convert String Object to Boolean Object


Here we are gonna see two methods of String to convert Boolean object.
we are checking here how to perform functionality of Boolean while having data in string
class StringObj{
   public static void main(String[] args) {
      // String Object
      String strg = "true";
      String strg1 = "false";
    
       //Passing string value to the constructor of Boolean class.       
      Boolean obj = new Boolean(strg);
      Boolean obj1 = new Boolean(strg1);
      System.out.println(obj);
      System.out.println(obj1);
    
      // Using valueOf() method of Boolean class
      
      Boolean bobj2 = Boolean.valueOf(str);
      Boolean bobj3 = Boolean.valueOf(str1);
      System.out.println(bobj2);
      System.out.println(bobj3);
  }  
}
Output:
false
true
false
true
Boolean valueOf(String s)
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”.

Comments