File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pg no 555-K and B hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pg no 555-K and B hashCode()" Watch "pg no 555-K and B hashCode()" New topic

pg no 555-K and B hashCode()

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69

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

Joined: Dec 08, 2010
Posts: 1545

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.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
I agree. Here's the link:
subject: pg no 555-K and B hashCode()
It's not a secret anymore!