java.lang.Boolean getBoolean(String name)

Description

The getBoolean(String name) method of Boolean class returns true if and only if the system property named by the argument exists and is equal to the string “true”. (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) 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.

Important Notes:

  • throws SecurityException – for the same reasons as System.getProperty

Method Syntax

public static boolean getBoolean(String name)

Method Argument

Data Type Parameter Description
Boolean b the Boolean instance to be compared

Method Returns

The getBoolean(String name) method of Boolean class the boolean value of the system property.

Java Boolean getBoolean(String name) Example

Below is a simple java example on the usage of getBoolean() method of Boolean class.

package com.javatutorialhq.java.examples;

import java.util.Properties;

/*
 * This example source code demonstrates the use of 
 * getBoolean(String name) method of Boolean class.
 */

public class BooleanGetBooleanExample {

	public static void main(String[] args) {
		// call a method to set a property
		setProperty();
		// check if the property is existing
		// and it is equal to true
		Boolean testBoolean = Boolean.getBoolean("test.boolean");		
		
		// print the result
		System.out.println("Result:"+testBoolean);
		
	}
	/*
	 * This is a method to set a property
	 * to test out the getBoolean method
	 */
	public static void setProperty(){
		// instantiate a new Properties
		// equal to system properties
		Properties prop = System.getProperties();
		
		// set a property with key as test.boolean
		// and value is true
		prop.setProperty("test.boolean", "true");
	}

}

Basically on the above example, we have set a system property with boolean value and we used the getBoolean() method in getting the set value.

Sample Output

Below is the sample output when you run the above example.

java Boolean getBoolean() example output