Java.lang Package Classes

Java Boolean - equals() Method

The java.lang.Boolean.equals() method returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.


public boolean equals(Object obj)


obj Specify the object to compare with.

Return Value

Returns true if the Boolean objects represent the same value; false otherwise.




In the example below, the java.lang.Boolean.equals() method is used to compare given Boolean objects for equality.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Boolean objects
    Boolean b1 = true;
    Boolean b2 = true;
    Boolean b3 = false;

    //checking Boolean objects for equality
    System.out.println("Is b1 == b2?: " + b1.equals(b2)); 
    System.out.println("Is b1 == b3?: " + b1.equals(b3));    

The output of the above code will be:

Is b1 == b2?: true
Is b1 == b3?: false

❮ Java.lang - Boolean