Boolean.valueOf() method in Java

valueOf() is a static method of the Boolean wrapper class, and it has two overloaded implementations –

Let’s look at them one by one.

public static Boolean valueOf(boolean b)
  • What does it do? It converts a boolean primitive value to a Boolean instance. It is also a preferred way to make the objects of the Boolean wrapper class because it provides better space and time performance.
  • What does it return? It returns the instance or the object of the Boolean wrapper class.
public class Codekru {

	public static void main(String[] args) {

		Boolean b1 = Boolean.valueOf(false);
		Boolean b2 = Boolean.valueOf(true);

		System.out.println("b1 value: " + b1);
		System.out.println("b2 value: " + b2);
	}

}

Output –

b1 value: false
b2 value: true
Internal Implementation of Boolean valueOf(boolean b) method
    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

Here, TRUE and FALSE are the Boolean class constants which create an object of the Boolean class.

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
Time complexity of Boolean valueOf(boolean b) method

The time complexity of the valueOf(boolean b) method is O(1) as it only uses a ternary operator and then creates the corresponding instances.

public static Boolean valueOf(String s)

  • What does it do? It will take a string argument and convert it into a corresponding Boolean instance.
  • What does it return? It returns a Boolean instance representing the value of the string passed in the argument.

So, the “true” string will be converted to a TRUE boolean instance, and all other string values will be converted to a FALSE boolean instance.

public class Codekru {

	public static void main(String[] args) {

		Boolean b1 = Boolean.valueOf("true");
		Boolean b2 = Boolean.valueOf("false");
		Boolean b3 = Boolean.valueOf("abc");
		Boolean b4 = Boolean.valueOf(null);

		System.out.println("b1 value: " + b1);
		System.out.println("b2 value: " + b2);
		System.out.println("b3 value: " + b3);
		System.out.println("b4 value: " + b4);
	}

}

Output –

b1 value: true
b2 value: false
b3 value: false
b4 value: false
Internal Implementation of Boolean valueOf(String s) method
    public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }

valueOf(String s) internally uses the parseBoolean(String s) method to parse the string first and then uses the ternary operator on the boolean value returned by the parseBoolean() method.

Time complexity of Boolean valueOf(String s) method

As valueOf(String s) method internally uses the parseBoolean() method, so time complexity of the valueOf() method will be same as that of parseBoolean() method.

parseBoolean() has a worst-case complexity of O(4) and a best-case complexity of O(1). Please look at this post to know more about the parseBoolean() method time complexity.

The worst-case time complexity of the valueOf(String s) method is O(4), and the best-case time complexity is O(1).

Please visit this link to learn more about the Boolean wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *