File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes class Object. Method equals(). Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "class Object. Method equals()." Watch "class Object. Method equals()." New topic
Author

class Object. Method equals().

Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
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).
)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16689
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
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
Sheriff

Joined: Sep 28, 2004
Posts: 16689
    
  19

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

Joined: May 30, 2011
Posts: 44
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Prakash Dahagam
Greenhorn

Joined: Jul 31, 2011
Posts: 7

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.

I found my heaven in java...

OCJP 6
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
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 Dahagam
Greenhorn

Joined: Jul 31, 2011
Posts: 7

Yeah your are correct. Different objects have different hash codes....
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
Thanks for help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32667
    
    4
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
Sheriff

Joined: Oct 13, 2005
Posts: 32667
    
    4
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class Object. Method equals().
 
Similar Threads
please explain the output?
How many Objects are garbage collected?
About an Object reference being passed to a method call
difference between equals() and ==