Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the BooleanUtils.isTrue method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring