my dog learned polymorphism*
The moose likes Java in General and the fly likes Confused in equals working and its overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Confused in equals working and its overriding" Watch "Confused in equals working and its overriding" New topic
Author

Confused in equals working and its overriding

Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
Hello,
by following code :


the result why a.equals(a1) is false ?
Please explain how equal and instanceof works with classes and objects.
How does equals work at the low level?
Pretty confused in the area.

Keep Moving Forward
Radovan Vukovic
Greenhorn

Joined: Nov 03, 2010
Posts: 19
have you check hashCode()
hashCode must generate equal values for equal objects
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

How does equals work at the low level?

I take this to mean "If I don't override the equals(Object) method from the Object class then how does equals() work for my class?"

In that case the equals() method from Object is used. And that method returns true if the two objects being compared are the same object, and false if they are different objects.

In your example you are comparing two different objects, so the equals() method returns false.

And no, declaring a hashCode method has no effect on the result of equals().
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Just to add to what Paul said.

References here mean the variable names. And Instances mean the actual object created with the "new" command.
The default implementation of equals() method (provided in the class Object)- checks if the 2 References are pointing to the same object on the heap.


You see that the default implementation doesnt actually check the contents of the objects these references point to, instead it just checks if these references point to the same object.

You can override the equals() method in your class and then provide a check to see if the contents are also equal.


Mohamed Sanaulla | My Blog
santosh dhulipala
Ranch Hand

Joined: Oct 21, 2010
Posts: 52
my doubt is
for comparison of two String's equal method check the contents
will it compare form the hascode genrated
as when creating strings it will verify if it already exist in the String pool if not create the new one and store the address






Thanks & Regards
Santosh Kumar.D

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
That applies to Strings and the wrapper classes (to a limited extent), but doesn't apply to your classes.

And I don't believe the String#equals(java.lang.Object) method uses the hash code. You would have to look in src.zip to be sure.
 
jQuery in Action, 2nd edition
 
subject: Confused in equals working and its overriding
 
Similar Threads
Why does th compiler allow changing the type of inherited instance/static variables.
fwd or backward ,don't know which reference??
"private" access
Wrapper, boxing == huh????
Object assignments