English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Program to Create a Boolean Variable from a String

Using theparseBoolean()method creates a boolean variable from a string. For a boolean variable, first get a boolean value, then pass the string to it using Boolean.parseBoolean().

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

inparseBoolean()Parsing a string parameter as a boolean value. If the string parameter is not null and equals to the string "true", regardless of case, then the returned boolean value represents true.

The same thing is done for FALSE.

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

Now let's see a complete example, which consists of a boolean variable made up of a string.

Example

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 Result

true
false