• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Confused in equals working and its overriding

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you check hashCode()
hashCode must generate equal values for equal objects
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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





 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic