CodesJava

Easy learning with example program codes

String to boolean in java

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

package com.codesjava;
 
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.codesjava;
 
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.

Please follow and like us:
error
Posted in Java   








Copyright © 2019 CodesJava DMCA.com Protection Status