Java Boolean equals () Method

The equals () method of the Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object; otherwise, it returns false.

In Java, the Boolean class is a wrapper class that encapsulates a boolean value. This class provides various methods to work with boolean values, including the equals() method. The equals() method is used to compare two Boolean objects for equality. It checks whether the argument passed to it is not null and is a Boolean object representing the same boolean value as the current object.

When we need to compare two Boolean objects in your Java code to see if they have the same boolean value, this method comes in handy. Boolean objects in Java are immutable, meaning their state cannot be modified after creation. This immutability ensures thread safety and simplifies programming by eliminating the need for synchronization when working with boolean values. However, it's essential to remember that immutability also implies that once a Boolean object is created, its value cannot be changed.

Syntax:

Parameters:

obj - The object to compare with

Return Value:

  • It returns true if the Boolean objects represent the same value.
  • It returns false if the Boolean objects represent different values.

Advantages of equals() Method

  1. Consistent Comparison: The function provides a standardized way to compare Boolean objects for equality, ensuring consistent behavior across the codebase.
  2. Null Safety: It handles null values gracefully, preventing NullPointerExceptions and simplifying error handling.
  3. Encapsulation: Encapsulates comparison logic within the Boolean class, promoting code maintainability by hiding implementation details.
  4. Object-Oriented Principles: Supports object-oriented principles like encapsulation and polymorphism, enabling Boolean objects to participate in equality comparisons like other Java objects.
  5. Compatibility: Widely used in Java collections and frameworks, allowing seamless integration and utilization of collection functionalities.

Equality Comparison Vs. Identity Comparison

It's important to understand the differences between identity comparison in Java (the == operator) and equality comparison (the equals() method). The == operator checks their memory locations or whether they correspond to the same object instance, whereas the equals() method compares the boolean values represented by two Boolean objects. Comprehending this differentiation is important in order to compose accurate and effective Java code.

Example 1

File Name: BooleanEqualsExample1.java

Test it Now

Output:

equals() method returns false

Explanation

A class called Main with a main() method is defined in the Java code that is provided. Two Boolean objects, b1 and b2, are created inside the main procedure and initialised with true and false values, respectively. Then, in order to compare their boolean values, it calls the equals() function on b1 and passes b2 as an argument. The equals() method will return false since b1 holds true and b2 holds false. It prints "equals() method returns false" to the console as a result. This code shows how to compare Boolean objects according to their boolean values using the equals() method.

Example 2

File Name: BooleanEqualsExample2.java

Output:

Enter your percentage.
60
Sorry! You have failed. You needed 4.0 more marks to pass.
Try again next year.

Explanation

The Java code that is provided defines a class called Main, which has a main function. Two Boolean objects called b1 and b2, initialised with true and false values, respectively, are created inside the main procedure. Next, it compares the two boolean values by calling the equals() function on b1 and passing in b2 as an input. As b1 is true and b2 is false, the equals() function will yield a false result. The code then outputs to the console, "equals() method returns false". The equals() method can be used to compare Boolean objects according to their boolean values, as this code illustrates.

Example 3

File Name: BooleanEqualsExample3.java

Output:

Enter two number.
78
0
Divide = 78/0
Division  is not possible.

Explanation

The user is prompted to enter two numbers using a scanner by the provided Java code. After that, an array called an is initialised to hold these integers. The input values are then read by iterating across the array. Next, it sets the values of two Boolean variables, b1 and b2, to true and false, respectively.

Taking into account the possibility of dividing by zero, the code determines the quotient and remainder of the division of the two input numbers. It sets b2 to true if the division is impossible (denominator equals zero). Lastly, it applies the equals() method to compare b1 and b2. It publishes the remainder and quotient of the division if they are not equal, otherwise it produces a message saying that division is not feasible.

Conclusion

To sum up, Java's Boolean class's equals () method is an essential tool for comparing two Boolean objects according to their boolean values. It guarantees that the comparison considers both the objects and the boolean values, returning false otherwise and true when both objects represent the same boolean value.

The approach improves the readability and maintainability of code by offering a uniform means of carrying out equality checks on Boolean objects. The equals () method can be used in a variety of settings, from simple applications involving the comparison of boolean values to more intricate ones involving division by zero, as demonstrated by the examples given. When dealing with boolean values, Java developers can design more dependable and resilient code by correctly understanding and utilizing the equals() function.


Next TopicJava Boolean




Latest Courses