Core Java

Difference Between Boolean.TRUE and true in Java

Hello. In this tutorial, we will understand the distinction between Boolean.True and true in Java for the proper utilization of boolean values.

1. Introduction

In Java, Boolean.TRUE and true are both representations of the boolean value true, but they differ in terms of their types.

1.1 Boolean.TRUE

  • Boolean.TRUE is a constant defined in the Boolean class in Java’s standard library. It is of type Boolean, which is a wrapper class for the primitive type boolean.
  • Boolean.TRUE is an object that represents the logical value “true”. It is used when you need to store or pass around the boolean value as an object, for example, in collections or method arguments that require objects.
  • Since Boolean.TRUE is an object, you can invoke methods on it. For example, you can call Boolean.TRUE.toString() to get the string representation of “true”.
  • It is defined as a public static final field: public static final Boolean TRUE = new Boolean(true);

1.2 true

  • true is a literal of the primitive type boolean in Java.
  • It directly represents the logical value “true” without any object wrapper.
  • Since true is a primitive value, you cannot invoke methods on it or store it in collections directly. It is typically used in conditional statements, boolean expressions, and primitive boolean variables.
  • true is simply a keyword in Java’s syntax, representing the boolean literal “true”.

In summary, Boolean.TRUE is an object of type Boolean that represents the boolean value “true”, while true is a primitive boolean literal.

1.3 Boolean.TRUE vs true

Here’s the difference between Boolean.TRUE and true.

Boolean.TRUEtrue
  • Boolean.TRUE is a constant defined in the Boolean class in Java’s standard library. It is of type Boolean, which is a wrapper class for the primitive type boolean.
  • Boolean.TRUE is an object that represents the logical value “true”. It is used when you need to store or pass around the boolean value as an object, for example, in collections or method arguments that require objects.
  • Since Boolean.TRUE is an object, you can invoke methods on it. For example, you can call Boolean.TRUE.toString() to get the string representation of “true”.
  • It is defined as a public static final field: public static final Boolean TRUE = new Boolean(true);
  • true is a literal of the primitive type boolean in Java.
  • It directly represents the logical value “true” without any object wrapper.
  • Since true is a primitive value, you cannot invoke methods on it or store it in collections directly. It is typically used in conditional statements, boolean expressions, and primitive boolean variables.
  • true is simply a keyword in Java’s syntax, representing the boolean literal “true”.

2. Working

2.1 Boolean.TRUE example

Here’s an example that demonstrates the usage of Boolean.TRUE:

Example

import java.util.ArrayList;
import java.util.List;

public class BooleanExample {
    public static void main(String[] args) {
        List booleanList = new ArrayList<>();

        booleanList.add(Boolean.TRUE);
        booleanList.add(false);
        booleanList.add(Boolean.TRUE);

        for (Boolean value : booleanList) {
            System.out.println(value);
        }
    }
}

2.1.1 Explanation of the code

In this example, we create an ArrayList called booleanList to store boolean values. We add three elements to the list: Boolean.TRUE, false, and Boolean.TRUE.

Using Boolean.TRUE allows us to store the logical value “true” as an object in the list. Since booleanList is a collection that expects objects, we use Boolean.TRUE to represent the value “true” as an object.

Finally, in the for loop, we iterate over the elements of booleanList and print each boolean value. The output will be:

Output

true
false
true

2.2 true example

Here’s an example that demonstrates the usage of true:

Example

public class TrueExample {
    public static void main(String[] args) {
        boolean isTrue = true;
        int number = 10;

        if (isTrue) {
            System.out.println("The condition is true.");
            number += 5;
        }

        System.out.println("Number: " + number);
    }
}

2.2.1 Explanation of the code

In this example, we declare a boolean variable isTrue and initialize it with the value true.

We then have an if statement that checks the value of isTrue. If isTrue is true, the code within the if block will be executed. In this case, it prints “The condition is true.” to the console and increments the number variable by 5.

Finally, outside the if statement, we print the value of the number to the console. Since the condition in the if statement is true, the code within the if block executes, and the number becomes 15. Therefore, the output will be:

Output

The condition is true.
Number: 15

In this example, true is used as a boolean literal in a conditional statement to determine the flow of execution based on the condition being true or false.

This concludes our tutorial, and I trust that the article provided you with the information you sought. I wish you happy learning and encourage you to share your newfound knowledge with others!

3. Conclusion

In conclusion, Boolean.TRUE and true are both representations of the boolean value “true” in Java, but they differ in terms of their types and usage.

Boolean.TRUE is an object of type Boolean, which is a wrapper class for the primitive type Boolean. It is typically used when you need to store or pass around the boolean value as an object, such as in collections or method arguments that require objects. Since Boolean.TRUE is an object, you can invoke methods on it. For example, you can call `Boolean.TRUE.toString()` to obtain the string representation of “true”. It is defined as a public static final field within the Boolean class.

On the other hand, `true` is a boolean literal representing the primitive type `boolean`. It directly represents the logical value “true” without any object wrapper. It is commonly used in conditional statements, boolean expressions, and primitive boolean variables. Since `true` is a primitive value, you cannot invoke methods on it or store it in collections directly.

In summary, the choice between Boolean.TRUE and true depend on the context and requirements of your code. If you need to work with boolean values as objects or utilize the methods provided by the Boolean class, Boolean.TRUE is appropriate. However, if you are working with primitive boolean values in simple boolean operations or conditional statements, using the `true` boolean literal is sufficient.

4. Download the Files

This was a tutorial to understand the difference between two Boolean representations i.e. Boolean.TRUE and true.

Download
You can download the files of this example here: Difference Between Boolean.TRUE and true in Java

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button