What is the BooleanUtils.isTrue method in Java?

The isTrue static method of the BooleanUtils class can be used to check if the passed Boolean value is equal to true.

Syntax

public static boolean isTrue(Boolean bool);

Parameters

The isTrue method takes a Boolean object as an argument.

Return value

isTrue returns true if the passed argument is equal to true. Otherwise, the method returns false.

If we pass null, then isTrue returns false.

Import BooleanUtils

BooleanUtils is defined in the Apache Commons Lang package.

Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

Possible case

BooleanUtils.isTrue

Case

Result

BooleanUtils.isTrue(Boolean.TRUE)

true

BooleanUtils.isTrue(Boolean.FALSE)

false

BooleanUtils.isTrue(null)

false

Code

In the below code, we use the isTrue method as follows:

import org.apache.commons.lang3.BooleanUtils;
class IsTrueExample {
public static void main( String args[] ) {
System.out.println("BooleanUtils.isTrue(Boolean.TRUE) => " + BooleanUtils.isTrue(Boolean.TRUE)); // true
System.out.println("BooleanUtils.isTrue(Boolean.FALSE) => " + BooleanUtils.isTrue(Boolean.FALSE)); // false
System.out.println("BooleanUtils.isTrue(null) => " + BooleanUtils.isTrue(null)); // false
}
}

Explanation

In the above code:

  • In line 1, we import the BooleanUtils class.
import org.apache.commons.lang3.BooleanUtils;
  • From lines 5 to 8, we use the isTrue method to check if the passed argument is true.
  • Below is the output.
BooleanUtils.isTrue(Boolean.TRUE); // true

BooleanUtils.isTrue(Boolean.FALSE); // false

BooleanUtils.isTrue(null); // false

Free Resources