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-langpackage, 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)); // trueSystem.out.println("BooleanUtils.isTrue(Boolean.FALSE) => " + BooleanUtils.isTrue(Boolean.FALSE)); // falseSystem.out.println("BooleanUtils.isTrue(null) => " + BooleanUtils.isTrue(null)); // false}}
Explanation
In the above code:
- In line 1, we import the
BooleanUtilsclass.
import org.apache.commons.lang3.BooleanUtils;
- From lines 5 to 8, we use the
isTruemethod to check if the passed argument istrue. - Below is the output.
BooleanUtils.isTrue(Boolean.TRUE); // true
BooleanUtils.isTrue(Boolean.FALSE); // false
BooleanUtils.isTrue(null); // false