File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A queastion about equals () & hashCode() methods

 
Nasser Fayeghi Fard
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've written some code to investigate euals () & hashCode() methods. Here, I want to check if we have a hashCode method that always return a constant value, is equals() method return true?



I expected 'true', but it prints false. Does anybody have any idea about internal of equals () mehotd? In which condition I can manipulate (Object) methods that it returns true for two different objects?


Thanks for any idea
Nasseria
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expected 'true'

Why did you expect true, the equals method in the Object class doesn't use the hashCode method to find if objects are equal or not. It uses a simple == comparison on objects, so it will return true only if you call it with the same object that you pass it
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Heap they are created as two different objects that's why it's false, as t1 and t2 are poinitng to two two different objects.
if you use t1=t2; in your code then it results true, as t1 and t2 points to same object on Heap.
 
Aj Deschanel
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Specs say: If you do not override the "equals()" method, then your class will use "==" to check the objects equality. So for your particular case it's normal to get False.
It's very easy to understand if you take a look at the code of "equals()" method from "Object.java"
 
Salil Vverma
Ranch Hand
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nasser,

The simplest explanation of this behavior would be to identify the definition of equals function. As you have not overridden the objects equal method, t1.equals(t2) would use the equals function of Object class.

The equals method of Object class checks the equality using == operator and returns whether both the references are pointing to the same object or not.

Due to this you are getting false as a result. I hope, this would help you to get your doubts clear
 
Nasser Fayeghi Fard
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Deschanel wrote:The Specs say: If you do not override the "equals()" method, then your class will use "==" to check the objects equality. So for your particular case it's normal to get False.
It's very easy to understand if you take a look at the code of "equals()" method from "Object.java"


Thanks Aj Deschanel & Salil Vverma
It was exactly what I was looking for default behaviour of of equals () that as you mentioned is based on comparison between two references using ==

Thanks All guys for sharing
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic