Re: Over-riding equals method dilemma

From: Anony! (someone_at_something.com)
Date: 08/13/04


Date: Fri, 13 Aug 2004 13:19:09 GMT


"andrewh1" <andrewh1@iinet.net.au> wrote in message
news:411cb39c$0$16330$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
>
>
> Anony! wrote:
> > Hi
> >
> > <snippet>
> >
> > public boolean equals(Object obj)
> > {
> > if (obj == null) //*
> > return false;
> > else if (obj instanceof Node )
> > return this.identifier.equals(((Node)obj).identifier); //**
> > else return false;
> > }
> >
> > Problems:
>
> Why are the following problems?

Don't want to be rude, but can't you read? I'm trying to understand why I'm
getting these compile errors/runtime errors if the code is changed as
indicated below. And thanks to the reponses, I know why!

> > 1 ) if i change * to:
> >
> > if (obj.equals(null))
> >
> > it compiles but i get a runtime error. i thought if your comparing
objects
> > you should use equals method. null is an object isn't it?
> >
> null is not an object. What is wrong with what you have?
>
> > 2) if i change ** to:
> >
> > return this.identifier.equals(obj.identifier); //**
> >
> > i get a compile error
> > Dont understand why u need to cast it into a Node, since you know its
> > already a instanceof a Node from the previous statement.
>
> You know; but the compiler still needs to be told.
>
> As I said what is wrong with your original code?
>
> However note that 'instanceof' tests for null as well. ie it could be
> rewritten as
>
> public boolean equals(Object obj) {
> return (obj instanceof Node) &&
> (this.identifier.equals(((Node)obj).identifier)));
> }
>
> Note that as Tony mentioned you should override hashcode() too. In this
> case if identifier is simply a String then you could return the hashcode
> for identifier. This will fulfil the equals/hashcode contract.

And as i mentioned equals/hashcode contract was mentioned in a different
post, and has nothing to do with the subject in this post. And yes, I do
realize that you have to override both methods, which I have also indicated
in the other post. Anyhow, I do appreciate the help.

AAA

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004


Relevant Pages

  • Re: REPOST: extremely long link times
    ... > somebodies old code that had ALOT of this stuff, and to compile it took ... and it was a noticable change. ... the compiler will only create new OBJ for those files that ... This does leave me with a lot of Test projects all over the place, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Over-riding equals method dilemma
    ... > public boolean equals(Object obj) ... > it compiles but i get a runtime error. ... case if identifier is simply a String then you could return the hashcode ...
    (comp.lang.java.programmer)
  • Re: REPOST: extremely long link times
    ... and it's linking that's the problem - compiling's generally very ... somebodies old code that had ALOT of this stuff, and to compile it took ... only 1 OBJ will be recreated. ... This does leave me with a lot of Test projects all over the place, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: djgpp on 386
    ... Nice to have references for things that far back so memory can't get too ... generates .OBJ directly). ... to compile it, and even then, you have to copy gstab.h to stab.h ... have to use DOS NASM because it can't shell the Win32 one (at least ...
    (comp.os.msdos.djgpp)
  • Re: Create assembly obj to link with Windows objs
    ... >Using MASM 5.1 and 6.x, I am able to compile and create an obj file. ... So, assuming that you really do have 16-bit MASM code, your choices are ...
    (comp.lang.asm.x86)