Java Program to create a boolean variable from string

Java 8Object Oriented ProgrammingProgramming

Use the parseBoolean() method to create a boolean variable from a string. Here for boolean variable, firstly take a boolean and pass the string to it using Boolean.parseBoolean().

boolean val1 = Boolean.parseBoolean("TRUE");

The parseBoolean() 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".

Do the same for FALSE as well.

boolean val2 = Boolean.parseBoolean("FALSE");

Let us now see the complete example to form a boolean variable from string.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      boolean val1 = Boolean.parseBoolean("TRUE");
      System.out.println(val1);
      boolean val2 = Boolean.parseBoolean("FALSE");
      System.out.println(val2);
   }
}

Output

true
false
raja
Published on 13-Nov-2018 12:02:33
Advertisements