String to boolean in java

Java convert string to boolean using Boolean.parseBoolean() method

package com.w3spoint;
 
public class StringToBoolean {
  public static void main(String args[]){
      String str1 = "false";
      String str2 = "true";
      //convert string to boolean
      boolean var1 = Boolean.parseBoolean(str1);
      boolean var2 = Boolean.parseBoolean(str2);
 
      System.out.println("Boolean value of str1 is: "+var1);
      System.out.println("Boolean value of str2 is: "+var2);
  }
}

Output:

Boolean value of str1 is: false
Boolean value of str2 is: true

Download this example.

Java convert string to boolean using Boolean.valueOf() method

package com.w3spoint;
 
public class StringToBoolean {
  public static void main(String args[]){
      String str1 = "false";
      String str2 = "true";
      //convert string to boolean
      boolean var1 = Boolean.valueOf(str1);
      boolean var2 = Boolean.valueOf(str2);
 
      System.out.println("Boolean value of str1 is: "+var1);
      System.out.println("Boolean value of str2 is: "+var2);
  }
}

Output:

Boolean value of str1 is: false
Boolean value of str2 is: true

Download this example.

Content Protection by DMCA.com
Please Share