aspose file tools*
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
Author

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
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pg no 555-K and B hashCode()