• 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

Understanding Equals

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I compiled the following code and it always give "They are not equal.". How can I make the Objects equal? Can someone explain?



Also, if you have two references "x" and "y" that refer to instances of wrapper classes. Which two scenarios will return a true?



(edited long line so we don't have to scroll over to right)
[ July 25, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object APIs


The equals method for class Object implements the most discriminating
possible equivalence relation on objects; that is, for any non-null
reference values x and y, this method returns true if and only if x and y
refer to the same object (x == y has the value true).


[ July 25, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I make the Objects equal?



No way. Let's simplify that first code:


myObject refers to one instance of Object, and yourObject refers to another, quite distinct, instance of Object.

Now, the Object.equals(Object o) method simply uses the "==" comparison as if you wrote myObject == yourObject. And that is obviously false because the references refer to different objects.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above code returns true.....
Does the above explanation hold true for any other case where an object is compared with another object of the same type...for eg:
even(new EqualsScenario()).equals(new EqualsScenario()) returns false

but then why above code return true??? what kind of excpetion(not the Java exception) is this?
[ July 25, 2006: Message edited by: Suhas Wadadekar ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to my code above:

"myObject.equals(myObject)" is true and yourObject.equals(yourObject) is true. Why? Because "myObject == myObject" is true and "yourObject == yourObject" is true, respectively.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Integer class overriding the equals() method in Object class.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what about the first scenario in regard to wrapper objects:



This will always return true? The 2nd scenario "x.equals(y) returns true if x==y" is not always true as I've mentioned above.

What about the 3rd scenario?:

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suhas Wadadekar:


The above code returns true.....then why (new Object()).equals(new Object()) returns false?



Your example with Integer is different because Integer overrides Object.equals() with a version that compares the values contained in those two wrapper objects. Both Integer objects represent the value 5 so Integer.equals() returns true. There are still two unique objects involved.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the equals method is implemented properly it must be reflexive. That is, x.equals(x) for any x.

The equals method must also be symmetric. That is, if x.equals(y) then y.equals(x) for any x and y. So in the case of two Integer objects representing the same value 42 then it does not matter if you compare them one way around or the other.

If equals is implemented properly, then it must be transitive. That is, for any three objects x,y, and z; if x.equals(y) and y.equals(z), then x.equals(z).
[ July 25, 2006: Message edited by: Barry Gaunt ]
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So based on the comments so far: If you have two references "x" and "y" that refer to instances of wrapper classes.

Which TWO scenarios will return a true?


1)x.equals(x) returns true.

This statement is true as it satisfy the �reflexive� criteria.

2)x.equals(y) returns true if x==y

This is not always true as already mentioned:

Consider this:

Integer x = 5;
Float y = 5f;

Then x==y is true. But x.equals(y) will not compile because while primitives value are the same, they are not similar wrapper objects.

3)x.equals(y) may return false even if x.equals(y) return true.

Is this statement true eventhough it doesn�t satisfy the �symmetric� criteria?

4)x.equals(y) throws an exception if they are different wrapper objects.

It doesn�t throw exception but gave me a compilation error instead.

5)x.equals(y) return false if they are different wrapper objects.

Compilation error so not even sure if you can return a
false here.
[ July 25, 2006: Message edited by: Shanel Jacob ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Consider this:

Integer x = 5;
Float y = 5f;

Then x==y is true.



Is it true? I get the compile error: Incomparable types - java.lang.Integer and java.lang.Float.

x is a reference to an Integer object representing value 5
y is a reference to a Float object representing value 5.0f

There is no way that x and y refer to the same objects in the memory.
[ July 25, 2006: Message edited by: Barry Gaunt ]
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I must have gotten confused. That didn't compile either.

So I guess only this 2 scenarios return true:

1) x.equals(x) returns true.

2) x.equals(y) returns true if x==y
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. Let x and y be references to two objects.
If x == y, then you know that x and y refer to the same object in memory. So x and y must represent the same value. So x.equals(y) must be true.

The other way round is not so. If x.equals(y) is true, it does not mean that the two objects are the same object. You can have two different instances of Integer objects representing the same value. For instance, Integer x = new Integer(42); and Integer y = new Integer(42); these are two distinct objects ( x != y ) but with x.equals(y).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


3) x.equals(y) may return false even if x.equals(y) return true.

Is this statement true even though it doesn�t satisfy the �symmetric� criteria?



If equals is correctly implemented, then this statement must be false. x.equals(y) and y.equals(x) must return the same value, either both true or both false.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic