equals and hashCode

I wonder if my equals and hashCode are ok?

thanks for any comment/improvment

publicboolean equals(Object o)

{

if (this == o)

{

returntrue;

}

if (o ==null || getClass() != o.getClass())

{

returnfalse;

}

Car car = (Car) o;

Identification identification = car.getIdentification();

if (identification.getCarNumber()==this.identification.getCarNumber())

returntrue;

returnfalse;

}

publicint hashCode()

{

return (int) identification.getCarNumber();

}

[1424 byte] By [xianwinwina] at [2007-11-26 16:46:01]
# 1
Looks reasonable, as long as you're sure identification can never be null.Here's some good info on equals & hashcode. http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
jverda at 2007-7-8 23:13:24 > top of Java-index,Java Essentials,Java Programming...
# 2

Just a tweak: don't writeif (expr) {

return true;

} else {

return false;

}

when you can writereturn expr;

So your equals ispublic boolean equals(Object o) {

if (this == o) {

return true;

}

if (o == null || getClass() != o.getClass()) {

return false;

}

Car that = (Car) o;

return this.identification.getCarNumber() == that.getIdentification().getCarNumber();

}

DrLaszloJamfa at 2007-7-8 23:13:24 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks!
xianwinwina at 2007-7-8 23:13:24 > top of Java-index,Java Essentials,Java Programming...