Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Need clarification on Object.equals. (http://www.velocityreviews.com/forums/t955614-need-clarification-on-object-equals.html)

plewto@gmail.com 12-18-2012 08:13 AM

Need clarification on Object.equals.
 
In the following code Node is an abstract class and both Gate and
Monitor are extensions of Node. a and b are distinct objects yet
a.equals(b) is returning true.

public class Foo {
public static void main(String[] argv){
Node a = new Gate();
Monitor b = new Monitor();
System.out.println(a.equals(b)); // --> prints 'true'
}
}

The underlying production code is a bit complex to include here. My
understanding is that equals is true if, and only if, a and b are
exactly the same object. Here they are not even the same class.


I did do a test stripped down to the bare essence:

public class Foo {
public static void main(String[] argv){
Foo a = new Bar();
Foo b = new Bar();
System.out.println(a.equals(b)); // --> prints 'false'
}
}
class Bar extends Foo {}

In this case the results are as I expected, a.equals(b) --> false

What could be going on here?

java version 1.7.0_06 on 64-bit Fedora 17

Roedy Green 12-18-2012 12:48 PM

Re: Need clarification on Object.equals.
 
On Tue, 18 Dec 2012 10:39:27 +0000, lipska the kat
<lipskathekat@yahoo.co.uk> wrote, quoted or indirectly quoted someone
who said :

>> Node a = new Gate();
>> Monitor b = new Monitor();
>> System.out.println(a.equals(b)); // --> prints 'true'


Gate or one of its superclasses is implementing equals.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.

markspace 12-18-2012 06:24 PM

Re: Need clarification on Object.equals.
 
On 12/18/2012 12:13 AM, plewto@gmail.com wrote:
> Node a = new Gate();
> Monitor b = new Monitor();
> System.out.println(a.equals(b)); // --> prints 'true'

....
> The underlying production code is a bit complex to include here. My


This is a red flag. The code should not be so complex that you can't
show us what is really going on. If it *is* too complex, then likely
the issue is the complexity itself.

However, I think Roedy zeroed in on the most likely cause. Your
abstract class, Node (or some superclass), implements equals()
*incorrectly* and is returning true when it should not. Probably Node()
should not implement equals() at all.

Show us the implementation of equals() for Node (and probably Gate too,
that version of equals() could also be borked in the example you gave)
and we'll point out the error.



plewto@gmail.com 12-18-2012 06:48 PM

Re: Need clarification on Object.equals.
 
On Tuesday, December 18, 2012 12:24:44 PM UTC-6, markspace wrote:
> On 12/18/2012 12:13 AM,
>
> > Node a = new Gate();

>
> > Monitor b = new Monitor();

>
> > System.out.println(a.equals(b)); // --> prints 'true'

>
> ...
>
> > The underlying production code is a bit complex to include here. My

>
>
>
> This is a red flag. The code should not be so complex that you can't
>
> show us what is really going on. If it *is* too complex, then likely
>
> the issue is the complexity itself.
>
>
>
> However, I think Roedy zeroed in on the most likely cause. Your
>
> abstract class, Node (or some superclass), implements equals()
>
> *incorrectly* and is returning true when it should not. Probably Node()
>
> should not implement equals() at all.
>
>
>
> Show us the implementation of equals() for Node (and probably Gate too,
>
> that version of equals() could also be borked in the example you gave)
>
> and we'll point out the error.


It is complex because it is a large application. I can either post the several hundred lines of source or the the 6 which adequately illustrates the point. Node does not implement equals at all as you say

plewto@gmail.com 12-18-2012 06:56 PM

Re: Need clarification on Object.equals.
 
On Tuesday, December 18, 2012 4:39:27 AM UTC-6, lipska the kat wrote:
> On 18/12/12 08:13,
>
> > In the following code Node is an abstract class and both Gate and

>
> > Monitor are extensions of Node. a and b are distinct objects yet

>
> > a.equals(b) is returning true.

>
> >

>
> > public class Foo {

>
> > public static void main(String[] argv){

>
> > Node a = new Gate();

>
> > Monitor b = new Monitor();

>
> > System.out.println(a.equals(b)); // --> prints 'true'

>
> > }

>
> > }

>
>
>
> According to the documentation the contract for equals on instances of
>
> class Object is as follows
>
>
>
> "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)."
>
>
>
> This is obviously the not the case in your example above.
>
> Does the class Node or any of it's superclasses override the equals method ?
>
>
>
> The following code returns false as expected
>
>
>
> public abstract class Foo {
>
>
>
> public static void main(String args[]){
>
> Foo bar = new Bar();
>
> Baz baz = new Baz();
>
> System.out.println(bar.equals(baz));
>
> }
>
> }
>
>
>
> class Bar extends Foo{}
>
>
>
> class Baz extends Foo{}
>
>
>
> ...
>
>
>
> However, add the following method to the class Foo
>
>
>
> public boolean equals(Object obj){
>
> return true;
>
> }
>
>
>
> and re-run the code and we get true.
>
>
>
> Has someone been messing with equals ?
>
>
>
> lipska
>
>
>
> --
>
> Lipska the Kat�: Troll hunter, sandbox destroyer
>
> and farscape dreamer of Aeryn Sun


Thanks for your response,

I see where the problem is. I do not directly implement equals, however Node is an extension of AbstractSet which does redefine equals. As it turns out I was in the process of rewriting Node so that it no longer extends AbsteractSet when the anomaly popped up in test code, so it is actually a mote point.

David Lamb 12-18-2012 07:01 PM

Re: Need clarification on Object.equals.
 
On 18/12/2012 1:48 PM, plewto@gmail.com wrote:
> On Tuesday, December 18, 2012 12:24:44 PM UTC-6, markspace wrote:
>> Show us the implementation of equals() for Node (and probably Gate too,
>> that version of equals() could also be borked in the example you gave)
>> and we'll point out the error.

>
> It is complex because it is a large application. I can either post the several hundred lines
> of source or the the 6 which adequately illustrates the point. Node

does not implement equals
> at all as you say


Roedy suggested Gate, not Node, might implement "equals". Does it?

There's likely not much people can do to help without more context. The
"6 lines" don't adequately "illustrate the point" because from them
alone nobody can say for sure what your problem is. Roedy's guess might
be the best advice you're going to get.


plewto@gmail.com 12-18-2012 08:14 PM

Re: Need clarification on Object.equals.
 
On Tuesday, December 18, 2012 1:01:51 PM UTC-6, David Lamb wrote:
> On 18/12/2012 1:48 PM, p...mail.com wrote:
>
> > On Tuesday, December 18, 2012 12:24:44 PM UTC-6, markspace wrote:

>
> >> Show us the implementation of equals() for Node (and probably Gate too,

>
> >> that version of equals() could also be borked in the example you gave)

>
> >> and we'll point out the error.

>
> >

>
> > It is complex because it is a large application. I can either post the several hundred lines

>
> > of source or the the 6 which adequately illustrates the point. Node

>
> does not implement equals
>
> > at all as you say

>
>
>
> Roedy suggested Gate, not Node, might implement "equals". Does it?
>
>
>
> There's likely not much people can do to help without more context. The
>
> "6 lines" don't adequately "illustrate the point" because from them
>
> alone nobody can say for sure what your problem is. Roedy's guess might
>
> be the best advice you're going to get.


Yes I understand that. In fact, as I pointed out in a subsequent post, noneof my code defines equals, Node was however extending AbstractSet which does redefine it. Really All I was looking for was a general direction I might look and not to burden anyone with large blocks of code. Node is 212 lines, Gate is 67, Monitor another 85, none of which even once mentions the word "equals"

My issue with Roedy's response was not the helpful suggestion to look at super classes but rather that it comes off as lecturing, and frankly rather condescending.

plewto@gmail.com 12-18-2012 08:17 PM

Re: Need clarification on Object.equals.
 
On Tuesday, December 18, 2012 2:14:58 PM UTC-6, ple...@gmail.com wrote:
> On Tuesday, December 18, 2012 1:01:51 PM UTC-6, David Lamb wrote:
>
> > On 18/12/2012 1:48 PM, p...mail.com wrote:

>
> >

>
> > > On Tuesday, December 18, 2012 12:24:44 PM UTC-6, markspace wrote:

>
> >

>
> > >> Show us the implementation of equals() for Node (and probably Gate too,

>
> >

>
> > >> that version of equals() could also be borked in the example you gave)

>
> >

>
> > >> and we'll point out the error.

>
> >

>
> > >

>
> >

>
> > > It is complex because it is a large application. I can either post the several hundred lines

>
> >

>
> > > of source or the the 6 which adequately illustrates the point. Node

>
> >

>
> > does not implement equals

>
> >

>
> > > at all as you say

>
> >

>
> >

>
> >

>
> > Roedy suggested Gate, not Node, might implement "equals". Does it?

>
> >

>
> >

>
> >

>
> > There's likely not much people can do to help without more context. The

>
> >

>
> > "6 lines" don't adequately "illustrate the point" because from them

>
> >

>
> > alone nobody can say for sure what your problem is. Roedy's guess might

>
> >

>
> > be the best advice you're going to get.

>
>
>
> Yes I understand that. In fact, as I pointed out in a subsequent post, none of my code defines equals, Node was however extending AbstractSet which does redefine it. Really All I was looking for was a general direction I might look and not to burden anyone with large blocks of code. Node is 212 lines, Gate is 67, Monitor another 85, none of which even once mentions the word "equals"
>
>
>
> My issue with Roedy's response was not the helpful suggestion to look at super classes but rather that it comes off as lecturing, and frankly rathercondescending.


Im sorry I meant markspace's responce not Roedy's

Stefan Ram 12-18-2012 08:50 PM

Re: Need clarification on Object.equals.
 
plewto@gmail.com writes:
>Yes I understand that. In fact, as I pointed out in a
>subsequent post, none of my code defines equals, Node was
>however extending AbstractSet which does redefine it. Really
>All I was looking for was a general direction I might look


The general direction would be to look up the documentation
of »Gate«, especially Gate#equals.

>>>>> public static void main(String[] argv){
>>>>> Node a = new Gate();
>>>>> Monitor b = new Monitor();
>>>>> System.out.println(a.equals(b)); // --> prints 'true'
>>>>> }
>>>>>}
>>>>>
>>>>>The underlying production code is a bit complex to include
>>>>>here. My understanding is that equals is true if, and only if,
>>>>>a and b are exactly the same object. Here they are not even
>>>>>the same class.


The following also prints »true« for possibly good reasons
(depending on the semantics of the classes, which is not
given here). At least it shows that this can happen without
overriding »equals«.

class Node extends java.io.File{ public Node(){ super( "alpha" ); }}
class Gate extends Node{}
class Monitor extends Node{}

public class Main
{ public static void main( final java.lang.String[] args )
{ Node a = new Gate();
Monitor b = new Monitor();
java.lang.System.out.println( a.equals( b )); }}


Lew 12-18-2012 08:52 PM

Re: Need clarification on Object.equals.
 
ple...@com wrote:

> My issue with [markspace]'s response was not the helpful suggestion to look at super classes but rather that it comes off as lecturing, and frankly rather condescending.


Wow. You really are a piece of work.

You come here asking people to volunteer assistance to you out of the goodness of their hearts.

markspace wrote nothing at all to justify your remark. He was objective, and offered to review
your code for you. He gave you advice on how to present the code so that
kind volunteers can help. You respond with this garbage instead of thanks?

What the hell is wrong with you?

> My understanding is that equals is true if, and only if, a and b are
> exactly the same object. Here they are not even the same class.


That's your misunderstanding. How about you read the Java tutorials to find out
what the truth is about 'equals()'?

--
Lew


All times are GMT. The time now is 10:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.