Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is BooleanUtils.toStringTrueFalse in Java?

Programming Bytes

The toStringTrueFalse method of the BooleanUtils class converts a boolean value to its equivalent. For instance, toStringTrueFalse will return the string "true" if the passed argument is true, and the string "false" in case the boolean value is false. If the argument is null, then the method returns null.

Syntax

public static String toStringTrueFalse(Boolean bool);

Parameters

  • bool: the Boolean object to be converted to the string equivalent.

Return value

  • "true": If the argument is Boolean.TRUE.
  • "false": If the argument is Boolean.FALSE.
  • null: If the argument is null.

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.

You can import BooleanUtils as follows:

import org.apache.commons.lang3.BooleanUtils;
BooleanUtils.toStringTrueFalse

Case

Result

BooleanUtils.toStringTrueFalse(Boolean.TRUE)

"true"

BooleanUtils.toStringTrueFalse(Boolean.FALSE)

"fallse"

BooleanUtils.toStringTrueFalse(null)

null

Code

In the code below, we use the toStringTrueFalse method as follows.

import org.apache.commons.lang3.BooleanUtils;

class ToStringTrueFalseExample {
    public static void main( String args[] ) {
        System.out.println("BooleanUtils.toStringTrueFalse(Boolean.TRUE) => " + BooleanUtils.toStringTrueFalse(Boolean.TRUE)); // "true"
        System.out.println("BooleanUtils.toStringTrueFalse(Boolean.FALSE) => " + BooleanUtils.toStringTrueFalse(Boolean.FALSE)); // "false"
        System.out.println("BooleanUtils.toStringTrueFalse(null) => " + BooleanUtils.toStringTrueFalse(null)); // null
    }
}

Expected output

"BooleanUtils.toStringTrueFalse(Boolean.TRUE) => true"

"BooleanUtils.toStringTrueFalse(Boolean.FALSE) => false"

"BooleanUtils.toStringTrueFalse(null) => null"

Explanation

In the code above:

  • Line 1: We import the BooleanUtils class.
import org.apache.commons.lang3.BooleanUtils;
  • Lines 5 to 8: We use the toStringTrueFalse method to get the “true” or “false” string based on the passed argument.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring