Java Boolean - getBoolean() Method
The java.lang.Boolean.getBoolean() method returns true if and only if the system property named by the argument exists and is equal to the string "true". A system property is accessible through getProperty, a method defined by the System class.
If there is no property with the specified name, or if the specified name is empty or null, then false is returned.
Syntax
public static boolean getBoolean(String name)
Parameters
name |
Specify the system property name. |
Return Value
Returns the boolean value of the system property.
Exception
Throws SecurityException for the same reasons as System.getProperty.
Example:
In the example below, the java.lang.Boolean.getBoolean() method returns the system property.
import java.lang.*; public class MyClass { public static void main(String[] args) { //using System Class setProprty method //to set the system property test1, test2 System.setProperty("test1","true"); System.setProperty("test2","xyz"); //retrieve value of system properties //using System.getProperty String s1 = System.getProperty("test1"); String s2 = System.getProperty("test1"); System.out.println("System property for test1: " + s1); System.out.println("System property for test2: " + s2); //Printing boolean value of system properties boolean b1 = Boolean.getBoolean("test1"); boolean b2 = Boolean.getBoolean("test2"); System.out.println("boolean value of System property for test1: " + b1); System.out.println("boolean value of System property for test2: " + b2); } }
The output of the above code will be:
System property for test1: true System property for test2: true boolean value of System property for test1: true boolean value of System property for test2: false
❮ Java.lang - Boolean