
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Boolean getBoolean() Method
Description
The Java Boolean getBoolean(String name) 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.
Declaration
Following is the declaration for java.lang.Boolean.getBoolean() method
public static boolean getBoolean(String name)
Parameters
name − the system property name
Return Value
This method returns the boolean value of the system property.
Exception
NA
Getting Boolean using System property as True Example
The following example shows the usage of Boolean getBoolean() method where System property exists as true and a random string "abcd". In this program, we've created two boolean variables. Using System.setProperty(), we're created two system properties demo1 and demo2 with value as true and abcd. Now using getBoolean() method, values of system properties are retrieved and result is printed.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 2 boolean primitives bool1, bool2 boolean bool1, bool2; /** * using System class's setProprty method, set the values of * system properties demo1, demo2. */ System.setProperty("demo1","true"); System.setProperty("demo2","abcd"); // retrieve value of system properties to s1, s2 String s1 = System.getProperty("demo1"); String s2 = System.getProperty("demo2"); // assign result of getBoolean on demo1, demo2 to bool1, bool2 bool1 = Boolean.getBoolean("demo1"); bool2 = Boolean.getBoolean("demo2"); String str1 = "boolean value of system property demo1 is " + bool1; String str2 = "System property value of demo1 is " + s1; String str3 = "boolean value of system property demo2 is " + bool2; String str4 = "System property value of demo2 is " + s2; // print bool1, bool2 and s1, s2 values System.out.println( str1 ); System.out.println( str2 ); System.out.println( str3 ); System.out.println( str4 ); } }
Output
Let us compile and run the above program, this will produce the following result −
boolean value of system property demo1 is true System property value of demo1 is true boolean value of system property demo2 is false System property value of demo2 is abcd
Getting Boolean using System property Example
The following example shows the usage of Boolean getBoolean() method where System property exists as false and a random string "abcd". In this program, we've created two boolean variables. Using System.setProperty(), we're created two system properties demo1 and demo2 with value as false and abcd. Now using getBoolean() method, values of system properties are retrieved and result is printed.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 2 boolean primitives bool1, bool2 boolean bool1, bool2; /** * using System class's setProprty method, set the values of * system properties demo1, demo2. */ System.setProperty("demo1","false"); System.setProperty("demo2","abcd"); // retrieve value of system properties to s1, s2 String s1 = System.getProperty("demo1"); String s2 = System.getProperty("demo2"); // assign result of getBoolean on demo1, demo2 to bool1, bool2 bool1 = Boolean.getBoolean("demo1"); bool2 = Boolean.getBoolean("demo2"); String str1 = "boolean value of system property demo1 is " + bool1; String str2 = "System property value of demo1 is " + s1; String str3 = "boolean value of system property demo2 is " + bool2; String str4 = "System property value of demo2 is " + s2; // print bool1, bool2 and s1, s2 values System.out.println( str1 ); System.out.println( str2 ); System.out.println( str3 ); System.out.println( str4 ); } }
Output
Let us compile and run the above program, this will produce the following result −
boolean value of system property demo1 is false System property value of demo1 is false boolean value of system property demo2 is false System property value of demo2 is abcd
Getting Boolean using Non-Existent System property Example
The following example shows the usage of Boolean getBoolean() method where System property is not present. In this program, we've created two boolean variables. Now using getBoolean() method, values of system properties are retrieved and result is printed. As system properties are not present, false is returned by getBoolean method.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 2 boolean primitives bool1, bool2 boolean bool1, bool2; // retrieve value of system properties to s1, s2 String s1 = System.getProperty("demo1"); String s2 = System.getProperty("demo2"); // assign result of getBoolean on demo1, demo2 to bool1, bool2 bool1 = Boolean.getBoolean("demo1"); bool2 = Boolean.getBoolean("demo2"); String str1 = "boolean value of system property demo1 is " + bool1; String str2 = "System property value of demo1 is " + s1; String str3 = "boolean value of system property demo2 is " + bool2; String str4 = "System property value of demo2 is " + s2; // print bool1, bool2 and s1, s2 values System.out.println( str1 ); System.out.println( str2 ); System.out.println( str3 ); System.out.println( str4 ); } }
Output
Let us compile and run the above program, this will produce the following result −
boolean value of system property demo1 is false System property value of demo1 is null boolean value of system property demo2 is false System property value of demo2 is null