This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 1502
    
    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)
 
wood burning stoves
 
subject: pg no 555-K and B hashCode()
 
Similar Threads
program needs to be explained
Static variables are never saved during Serialization, yet my code seems to do the opposite
a doubt in hashcode program
this.moofvalue
HashMap Doubt