Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

equal() method of class Object  RSS feed

 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the following program.....
when we create the two object of main class .
question :

by executing the following if((m2.equals(m)) the statement "false" gets executed

can you tell me why ???



public class Main {

public static void main(String[] args) {

Main m = new Main();
Main m2 = new Main ();

if(m2.equals(m))
{
System.out.println("true");
}
else
{
System.out.println("false");
}



}
}
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell me why ???


The implementation of the equals() method in the Object class check to see if they are the same object. Otherwise, they are not equal.

Henry
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear deeksha

When equals method is called on some object

As m2.equals(m)

first of all , it checks hashCode of both objects . If hashCode is same , only then equalality is checked as per default bahaviour or defined by you .
In your program , you have not overrided hashCode() method defined in Object class . Hecne both objects get different hashode and hence equals method returns false .
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhukar, what you write is not correct.

There is nothing special about the equals() method. It is an ordinary public method with the usual behaviours.

An implementation is provided in java.lang.Object. It does not compare hash codes. It compares the object references for equality. That is, it checks whether two object references point to the same object.

A subclass (*) can override equals(). The override can do absolutely anything. However, to obey the contract of equals(), it should return true when the superclass does, and possibly in some additional cases.

Note that getting appropriate behaviour of equals() in a complicated hierarchy of objects is difficult to impossible. Comparing objects of the exact same class is usually fine. Comparing objects where one is a subclass and the other a base class is fraught.

(*) of course, everything is a subclass of Object
[ August 21, 2007: Message edited by: Peter Chase ]
 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whst is hash code of an object.???
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hashCode
public int hashCode()Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


Returns:
a hash code value for this object.

Hope this document from the API is helpful to you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!