Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need clarification on Object.equals.

Reply
Thread Tools

Need clarification on Object.equals.

 
 
plewto@gmail.com
Guest
Posts: n/a
 
      12-18-2012
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-18-2012
On Tue, 18 Dec 2012 10:39:27 +0000, lipska the kat
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      12-18-2012
On 12/18/2012 12:13 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


 
Reply With Quote
 
plewto@gmail.com
Guest
Posts: n/a
 
      12-18-2012
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
 
Reply With Quote
 
plewto@gmail.com
Guest
Posts: n/a
 
      12-18-2012
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.
 
Reply With Quote
 
David Lamb
Guest
Posts: n/a
 
      12-18-2012
On 18/12/2012 1:48 PM, (E-Mail Removed) 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.

 
Reply With Quote
 
plewto@gmail.com
Guest
Posts: n/a
 
      12-18-2012
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.
 
Reply With Quote
 
plewto@gmail.com
Guest
Posts: n/a
 
      12-18-2012
On Tuesday, December 18, 2012 2:14:58 PM UTC-6, (E-Mail Removed) 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-18-2012
(E-Mail Removed) 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 )); }}

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-18-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need clarification on a function declaration wizwx C Programming 9 02-13-2007 03:19 PM
need clarification with import statements Tool69 Python 2 12-15-2006 11:56 AM
Need clarification on 'typedef' keyword Krishna C++ 5 04-01-2005 05:21 PM
Need clarification on asp_wp.exe,aspnet_wp.exe and aspnet_isapi.dll prem ASP .Net 1 11-03-2004 05:32 AM
Need some clarification and/or help with java NIO sockets HDSkiFreak Java 2 05-11-2004 01:48 PM



Advertisments