Boolean equals() method in Java with examples


The equals() method of Boolean class is a built in method of Java which is used check equality of two Boolean object.

Syntax:

BooleanObject.equals(Object ob)

Parameter: It take a parameter ob of type Object as input which is the instance to be compared.



Return Type: The return type is boolean. It returns true if the specified Object ‘ob’ has same value as the ‘BooleanObject’, else it returns false.

Below are programs to illustrate the equals() method of Boolean class:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to implement
// equals() method of Boolean class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // Boolean object
        Boolean a = new Boolean(true);
  
        // Boolean object
        Boolean b = new Boolean(true);
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + a.equals(b));
    }
}

chevron_right


Output:

true comparing with true = true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to implement
// equals() method of Java class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // Boolean object
        Boolean a = new Boolean(true);
  
        // Boolean object
        Boolean b = new Boolean(false);
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + a.equals(b));
    }
}

chevron_right


Output:

true comparing with false = false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to implement
// equals() method of Java class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // Boolean object
        Boolean a = new Boolean(false);
  
        // Boolean object
        Boolean b = new Boolean(true);
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + a.equals(b));
    }
}

chevron_right


Output:

false comparing with true = false


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.