java.lang.Boolean valueOf(boolean b)

Description

The valueOf(boolean b) method of Boolean class returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.

Make a note that the valueOf(boolean b) method of Boolean class is static thus it should be accessed statically. What I mean to say is that we should use this method such as below:

Boolean.valueOf(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java valueOf(boolean b) method non statically.

Method Syntax

public static Boolean valueOf(boolean b)

Method Argument

Data Type Parameter Description
boolean b a boolean value.

Method Returns

The valueOf(boolean b) method of Boolean class returns a Boolean instance representing b.

Java Boolean valueOf(boolean b) Example

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

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * valueOf(boolean b) method of Boolean class.
 */

public class BooleanValueOfbooleanExample {

	public static void main(String[] args) {	
		
		// initialize a new boolean primitive
	
		boolean boolPrimitive = true;	
		
		// convert boolean primitive to Boolean object
		Boolean boolObject = Boolean.valueOf(boolPrimitive);
		
		// test equality of the two value
		if(boolPrimitive == boolObject){
			System.out.println("they are equal");
		}
		else{
			System.out.println("they are not equal");
		}
		
	}

}

Sample Output

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

java Boolean valueOf(boolean b) example output