Home » Java programming language

Java Object Class boolean equals(Object o) method with Example

Java Object class boolean equals(Object o) method: Here, we are going to learn about the boolean equals(Object o) method of Object class with its syntax and example.
Submitted by Preeti Jain, on June 25, 2019

Object Class boolean equals(Object o)

  • This method is available in package java.lang.Object.equals(Object o).
  • This method is used to check the object with the specified object.
  • This method returns true if both Object reference and value are the same else return false.

Syntax:

    boolean equals(Object o){
    }

Parameter(s):

We pass only one object as a parameter in the method of the Object.

Return value:

The return type of this method is boolean that means this method returns true if the reference and value of the object are the same.

Java program to demonstrate example of Object Class equals() method

import java.lang.Object;

public class ObjectClass {
    public static void main(String[] args) {

        // Create a new object of Integer type
        Integer in1 = new Integer(10);

        // Create a new object of Float type
        Float fl = new Float(10.f);

        // Create another object of Integer type
        Integer in2 = new Integer(20);

        // Create another object of Integer type
        Integer in3 = new Integer(10);

        // Compare Integer and float type reference and value 
        // and it returns true if both same else false
        System.out.println("The result of comparing Integer and Float using equals():" + in1.equals(fl));

        // Compare Integer and another Integer type reference 
        // and value and it returns true if both same else false
        System.out.println("The result of comparing Integer and another Integer using equals():" + in1.equals(in2));

        // Compare Integer and another Integer type reference 
        // and value and it returns true if both same else false
        System.out.println("The result of comparing Integer and another Integer using equals():" + in1.equals(in3));
    }
}

Output

D:\Programs>javac ObjectClass.java

D:\Programs>java ObjectClass
The result of comparing Integer and Float using equals():false
The result of comparing Integer and another Integer using equals():false
The result of comparing Integer and another Integer using equals():true



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.