Core Java

Check if Two Booleans Are Equal In Java

In Java, the boolean type is used to represent one of two possible values: true or false. Sometimes, you might need to check if two boolean values are equal. This is a common operation in many logical conditions or control structures. Let us delve into understanding how we can check if two booleans are equal in Java.

1. Ways to Check Equality of Boolean Values

There are several ways to check if two boolean values are equal in Java. We will discuss three main approaches:

package com.jcg.example;

public class BooleanEqualityCheck {

    public static void main(String[] args) {
        
        // Example 1: Using == operator (primitive boolean values)
        boolean value1 = true;  // Declare a primitive boolean value 'value1'
        boolean value2 = true;  // Declare a primitive boolean value 'value2'
        System.out.println("Using '==' operator:");  // Print statement indicating the comparison method used
        // Compare two primitive boolean values using the '==' operator
        if (value1 == value2) {  // If both booleans are equal
            System.out.println("The boolean values are equal.");  // Print equality message
        } else {
            System.out.println("The boolean values are not equal.");  // Print inequality message
        }

        System.out.println();  // Print an empty line for better readability

        // Example 2: Using Boolean.equals() method (Boolean objects)
        Boolean value3 = Boolean.valueOf(true);  // Declare a Boolean object 'value3' set to true
        Boolean value4 = Boolean.valueOf(true);  // Declare another Boolean object 'value4' set to true
        System.out.println("Using Boolean.equals() method:");  // Print statement indicating the comparison method used
        // Compare two Boolean objects using the 'equals()' method
        if (value3.equals(value4)) {  // If both Boolean objects are equal
            System.out.println("The Boolean objects are equal.");  // Print equality message
        } else {
            System.out.println("The Boolean objects are not equal.");  // Print inequality message
        }

        System.out.println();  // Print an empty line for better readability

        // Example 3: Using Boolean.compare() method (comparison of primitive boolean values)
        boolean value5 = false;  // Declare a primitive boolean value 'value5' set to false
        boolean value6 = false;  // Declare another primitive boolean value 'value6' set to false
        System.out.println("Using Boolean.compare() method:");  // Print statement indicating the comparison method used
        // Compare two primitive boolean values using the 'Boolean.compare()' method
        if (Boolean.compare(value5, value6) == 0) {  // If both values are equal (compare() returns 0 for equality)
            System.out.println("The boolean values are equal.");  // Print equality message
        } else {
            System.out.println("The boolean values are not equal.");  // Print inequality message
        }

        System.out.println();  // Print an empty line for better readability

        // Example 4: Using == operator with different boolean values
        boolean value7 = true;  // Declare a primitive boolean value 'value7' set to true
        boolean value8 = false;  // Declare another primitive boolean value 'value8' set to false
        System.out.println("Using '==' operator with different boolean values:");  // Print statement indicating the comparison method used
        // Compare two primitive boolean values using the '==' operator
        if (value7 == value8) {  // If both booleans are equal
            System.out.println("The boolean values are equal.");  // Print equality message
        } else {
            System.out.println("The boolean values are not equal.");  // Print inequality message
        }

        System.out.println();  // Print an empty line for better readability

        // Example 5: Using Boolean.equals() with different Boolean objects
        Boolean value9 = Boolean.valueOf(true);  // Declare a Boolean object 'value9' set to true
        Boolean value10 = Boolean.valueOf(false);  // Declare another Boolean object 'value10' set to false
        System.out.println("Using Boolean.equals() method with different Boolean objects:");  // Print statement indicating the comparison method used
        // Compare two Boolean objects using the 'equals()' method
        if (value9.equals(value10)) {  // If both Boolean objects are equal
            System.out.println("The Boolean objects are equal.");  // Print equality message
        } else {
            System.out.println("The Boolean objects are not equal.");  // Print inequality message
        }
    }
}

1.1 Code Explanation

The Java program BooleanEqualityCheck demonstrates various ways to check if two boolean values are equal. It contains multiple examples using different techniques: the == operator, the Boolean.equals() method, and the Boolean.compare() method.

  • Example 1: Using == operator with primitive boolean values: The program begins by defining two boolean variables, value1 and value2, both set to true. The program uses the == operator to compare these primitive boolean values. If they are equal, the message “The boolean values are equal.” is printed. Otherwise, it prints “The boolean values are not equal.”
  • Example 2: Using Boolean.equals() method with Boolean objects: In this example, two Boolean objects, value3 and value4, are created using the Boolean.valueOf() method. These objects are compared using the Boolean.equals() method. If the Boolean objects are equal, the message “The Boolean objects are equal.” is printed. If they are not equal, it prints “The Boolean objects are not equal.”
  • Example 3: Using Boolean.compare() method with primitive boolean values: This example compares two primitive boolean values, value5 and value6, which are both set to false. The Boolean.compare() method is used to compare them. If the return value is 0, it means the boolean values are equal, and the message “The boolean values are equal.” is printed. Otherwise, the message “The boolean values are not equal.” is printed.
  • Example 4: Using == operator with different boolean values: In this example, two boolean variables, value7 and value8, are compared using the == operator. value7 is set to true and value8 is set to false. Since these values are not equal, the program prints “The boolean values are not equal.”
  • Example 5: Using Boolean.equals() method with different Boolean objects: Finally, two Boolean objects, value9 and value10, are compared using the Boolean.equals() method. value9 is set to true and value10 is set to false. As these values differ, the program prints “The Boolean objects are not equal.”

1.2 Code Output

The output produced by the code when executed is as follows:

Using '==' operator:
The boolean values are equal.

Using Boolean.equals() method:
The Boolean objects are equal.

Using Boolean.compare() method:
The boolean values are equal.

Using '==' operator with different boolean values:
The boolean values are not equal.

Using Boolean.equals() method with different Boolean objects:
The Boolean objects are not equal.

2. Comparison of Approaches

Now, let’s compare the three approaches:

  • == Operator: This is the most straightforward and commonly used method, especially when working with primitive boolean values. It provides a simple, concise, and efficient way to compare two booleans. However, it is limited to primitive types, meaning it cannot be used for comparing Boolean objects.
  • Boolean.equals() Method: This method is particularly useful when dealing with Boolean objects rather than primitive booleans. It works well when you need to compare wrapped booleans, such as when using the Boolean class instances. Since the equals() method checks for equality of the boolean value inside the object, it can be relied on for object-based comparisons, where the == operator would only check reference equality.
  • Boolean.compare() Method: This method is more versatile and flexible compared to the previous two, especially when you need to handle different boolean values programmatically. It is particularly useful when the comparison result needs to be processed further, such as for sorting or making complex decisions in logic. Boolean.compare() returns an integer value, which makes it suitable for use in algorithms requiring ordering, such as those found in sorting operations.

3. Conclusion

In Java, there are several ways to check if two boolean values are equal, each with its own use case:

  • Use the == operator when comparing primitive boolean values.
  • Use Boolean.equals() for comparing Boolean objects.
  • Use Boolean.compare() when you need a more general-purpose comparison method that handles boolean values with return values.

Choosing the right approach depends on the types of variables you’re working with and the requirements of your program. Understanding these methods will allow you to write efficient and readable Java code for boolean value comparison.

Yatin Batra

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