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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.