Tutorialspoint examples

Learn java tutorial with examples for beginners online

Convert string to boolean java


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

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

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status