Java Boolean getBoolean() Method
The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false.
Syntax
public static boolean getBoolean(String name)
Parameters
The parameter ‘name’ represents the name of the system property.
Return Value:
The getBoolean() method returns the Boolean value for the string value.
Example 1
public class JavaBooleanGetBooleanMethodExample1 { public static void main(String[] args) { //Assign value to val1 using java.lang.System.setProperty() System.setProperty("val1","true"); Boolean b1=Boolean.getBoolean("val1"); System.out.println(b1); } }
Output
true
Example 2
import java.util.Scanner; public class JavaBooleanGetBooleanMethodExample2 { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); boolean val1 = false; System.out.println("Enter the total no of people in the lift."); int a =scanner.nextInt(); if(a>=6){ System.setProperty("val1","true"); } boolean b2 =Boolean.getBoolean("val1"); System.out.println("value of val is "+b2); if(b2){ System.out.println("Sorry! Its out of lift's range. "); } else{ System.out.println("It’s ok. Let’s proceed."); } } }
Output
Enter the total no of people in the lift. 5 value of val is false It’s ok. Let’s proceed.
Example 3
public class JavaBooleanGetBooleanMethodExample3 { public static void main(String[] args) { //for any value other than true it will return false System.setProperty("val1","abc"); Boolean b1=Boolean.getBoolean("val1"); System.out.println(b1); } }
Output:
false