File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pg no 555-K and B hashCode()

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class SaveMe implements Serializable{
transient int x;
int y;
SaveMe(int xVal, int yVal) {
x = xVal;
y = yVal;
}
public int hashCode() {
return (x ^ y); // Legal, but not correct to
// use a transient variable
}
public boolean equals(Object o) {
SaveMe test = (SaveMe)o;
if (test.y == y && test.x == x) { // Legal, not correct
return true;
} else {
return false;
}
}
}
my doubt is

test.y refers to which y
and test.x refers to which x
please explain in detail
thanks in advance
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello kiruthigha rajan,

Please UseCodeTags.

As you can see, Object o has been casted to SaveMe. So, test.x is x of that object (o) and test.y is y of it (o).

I hope this helps.

By the way is this the code from Sierra's book? I wonder why there is no instanceof test in equals method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic