Core Java

Boolean.TRUE vs. true in Java

In Java, the boolean data type represents a value that can be either true or false. The two most commonly used representations of the boolean value true are Boolean.TRUE and true. While they both represent the same logical value, they have some differences in terms of usage and behavior. This article aims to explore these differences and provide insights into when to use each representation.

1. Introduction

When working with boolean values in Java, it’s important to understand the distinction between Boolean.TRUE and true. In essence, Boolean.TRUE is a constant defined in the java.lang.Boolean class, while true is a literal value.

Boolean TRUE_CONSTANT = Boolean.TRUE;
boolean trueLiteral = true;

In this section, we’ll delve deeper into each representation and examine their characteristics.

2. Boolean.TRUE

Boolean.TRUE is a static constant defined in the java.lang.Boolean class. It represents the boolean value true and provides additional functionality through the Boolean class.

Boolean booleanObj = Boolean.TRUE;

By using Boolean.TRUE, you can leverage the methods and features provided by the Boolean class. For example, you can use the toString() method to obtain a string representation of the boolean value.

System.out.println(Boolean.TRUE.toString()); // Output: "true"
Fig. 1: Boolean.true in Java.
Fig. 1: Boolean.true in Java.

Another advantage of using Boolean.TRUE is that it allows you to perform operations such as assignment, comparison, and method invocation. This is particularly useful when you need to pass a boolean value to a method that requires a Boolean object.

void someMethod(Boolean flag) {
    // Method implementation
}

someMethod(Boolean.TRUE); // Passing Boolean.TRUE as an argument

3. true

The true keyword is a boolean literal value in Java. It directly represents the boolean value true without any additional functionality.

boolean trueLiteral = true;

When using true, you are working directly with the boolean primitive type. This means that you cannot invoke methods or perform operations specific to the Boolean class.

// Invalid usage with true literal
// System.out.println(true.toString()); // Compilation error
Fig. 2: true in Java.
Fig. 2: true in Java.

However, working with the boolean primitive type can be more efficient in terms of memory and performance compared to using Boolean objects.

4. Boolean.TRUE vs. true

While Boolean.TRUE and true both represent the boolean value true, they have some differences in terms of usage and behavior. Let’s explore these differences in detail.

4.1. Type

Boolean.TRUE is an object of the java.lang.Boolean class, while true is a boolean primitive type. This distinction affects how they can be used in certain contexts.

For example, Boolean.TRUE can be used in places where an object is expected, such as method parameters or collections that store objects.

On the other hand, true can be used directly as a boolean value or assigned to a boolean variable.

boolean flag = true;

4.2. Memory Usage

Using Boolean.TRUE consumes more memory compared to using true. When you use Boolean.TRUE, an object of the Boolean class is created, which requires additional memory to store the object header and other fields.

In contrast, using true directly as a boolean value requires only the memory necessary to store a single bit.

4.3. Equality Comparison

When comparing Boolean.TRUE with true, the result may differ based on the comparison method used.

The == operator compares object references, so Boolean.TRUE == true would return false. However, using the equals() method performs a value-based comparison, so Boolean.TRUE.equals(true) would return true.

System.out.println(Boolean.TRUE == true); // Output: false
System.out.println(Boolean.TRUE.equals(true)); // Output: true

4.4. Autoboxing and Unboxing

Autoboxing is the process of automatically converting a primitive type to its corresponding wrapper class object. Similarly, unboxing is the process of converting a wrapper class object back to its primitive type.

Autoboxing allows you to assign a boolean primitive value to a Boolean object using Boolean.TRUE.

Boolean booleanObj = Boolean.TRUE;
boolean primitiveValue = booleanObj; // Unboxing

However, when using true directly as a boolean value, autoboxing is not involved.

boolean primitiveValue = true;

5. Use Cases

The choice between Boolean.TRUE and true depends on the specific use case and requirements of your program. Here are some scenarios where one representation might be preferred over the other.

  • Use Boolean.TRUE when you need to pass a boolean value to a method that requires a Boolean object.
void someMethod(Boolean flag) {
    // Method implementation
}

someMethod(Boolean.TRUE);
  • Use true when working with boolean values directly and performance or memory efficiency is a concern.
boolean flag = true;
  • Use Boolean.TRUE when you need to store boolean values in collections that require objects.
  • Use true when performing boolean comparisons or assignments within your code.
boolean flag = someCondition() ? true : false;

6. Conclusion

In Java, Boolean.TRUE and true both represent the boolean value true, but they have distinct characteristics and usage scenarios.

Boolean.TRUE provides additional functionality through the Boolean class, allowing you to perform operations and pass boolean values to methods that expect Boolean objects. However, it consumes more memory compared to using true directly as a boolean value.

On the other hand, true is a boolean literal value and works directly with the boolean primitive type. It offers better memory and performance efficiency but lacks the additional functionality provided by the Boolean class.

Choosing between Boolean.TRUE and true depends on the specific requirements of your program. Consider factors such as the need for object-oriented functionality, memory efficiency, and performance when making your decision.

7. Download the Source Code

Download
You can download the full source code of this example here: Boolean.TRUE vs. true in Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button