Boolean.booleanValue() method in Java

booleanValue() simply returns the boolean value of a Boolean class instance. This post will discuss the booleanValue() method of the Boolean wrapper class in detail.

  • Method declaration – public boolean booleanValue()
  • What does it do? It returns the boolean value represented by a Boolean class’s instance. In short, it will tell what boolean value a Boolean instance hold.
    A Boolean instance can either hold a true or a false boolean value.
  • What does it return? It will return either a true or a false boolean value.
Code Example
public class Codekru {

	public static void main(String[] args) {

		Boolean b1 = Boolean.valueOf(false);

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

Output –

value of b1: false
Internal Implementation of the booleanValue() method

booleanValue() method returns the variable’s value which holds the boolean primitive value for the object.

    public boolean booleanValue() {
        return value;
    }
Time complexity of the booleanValue() method

The time complexity of the booleanValue() method is O(1).

What if a null object calls the booleanValue() method?

It will throw a NullPointerException, as illustrated by the below example.

public class Codekru {

	public static void main(String[] args) {

		Boolean b1 = null;

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

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because "b1" is null

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 [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.