• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class Object. Method equals().

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run this code:



I expected this result:


true
true
true



but... real result:


false
true
true



so.... new Object() NOT EQUALS new Object()???

Next step, I looked source code for Object class:



hmmm.... i see that for class Object method equals return the same result as a ==. Clean?

Why class Object has't normal implementing equals method?? (What mean normal?? Normal it is when
return true).
)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Franko wrote:
so.... new Object() NOT EQUALS new Object()???



My question is why should they be equal? The new operator created new objects -- based on your argument then every Object instance should be equal to every other Object instance, as they were all (almost all) created with the new operator.

Henry
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example:



return:

true



Yep, really we create two different String Objects, each object has his unique address in heap, but first and second string obj contains the same values - method equals check this and return

true

;

the same situation with to different objects Object class created with new. Different address but containts the same values... so equals must return

true

.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case of strings, there is stuff to compare -- two blank strings should be equal. In the case of objects, there is nothing to compare -- from your description, any two objects should be equal.

Henry
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or other example:

Imagine, that Object is a Empty Cap.

Than, create two Object or in our case to Empty Cup - one Cup hold in Left hand, second Cup hold in Right hand.

Look on Empty Cups. What different between? No, different - First Cup equals Second Cup. Result true, Henry Wong
, you agree with me?


So, I really cant understand why method equals return false for two obj create with new.

But, I have one idea: maybe, it is only some simple convention? nothing more?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With your example, you specify what cup equality means. That means that you override the equals method (and the hashCode method to stick to the contract), and make it return true if both objects are empty cups. This is done for a lot of classes, including String and Integer.

For any random Object, there is no specification about what equality means, and therefore == is used.
 
Greenhorn
Posts: 7
Firefox Browser Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why you are comparing "Object" class's references with String class refercence,

String class has a method called "equals( )" method that is implicitly overrides the Object class's "equals( )" method like this...



what i mean to say that String class has it's own overridden equals( ) method and Wrapper classes inherits equals( ) method, from Object class. So until unless you override the equals( ) method, two objects with the same value ( not applicable for Wrapper classes and String, StringBuffer, StringBuilder classes because they are considered as separate just like they are having pools) of same class will not be equal.
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, can you, please, once more to clarify the following question:

we can use contract:

equal objects must have equal hash codes

to instances class Object??

I investigated this moment and see, that to different Objects (I mean new Object() and new Object()) has different hashCode value.

If we can use contract for this Objects - all Ok, different hashCode - not equals.
 
Prakash Gnana Shiva
Greenhorn
Posts: 7
Firefox Browser Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah your are correct. Different objects have different hash codes....
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for help!
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Prakash Dahagam

You can find the details of the String#equals(Object) method by going into your Java™ installation folder and finding the file called src.zip. If you unzip it, you will find the code of the String class inside it. You can only reliably use the instanceof operator if the class is final. The String class is indeed final, but you will need other tests of type equality for non-final classes.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added code tags to your post, and you can see how much better it looks. You should also consider reducing that method to thisNotice the use of the == operator for value means it will only work reliably if value is a primitive.
Also you don't need the get method; value is accessible here.
Writing if (...) return true; else return false; is not regarded as good style.
Use spaces not tabs for indenting.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic