Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a doubt in hashcode program

 
sasank manohar
Ranch Hand
Posts: 186
  • 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;
}
}
}

well the doubt is simple,

in this if test,
test.y refers to which y
and
y refers to which y
, similarly
test.x refers to which x
and
x refers to which x
, i am confused as we have x's and y's in both class SaveMe which are

transient int x;
int y;

and also in

SaveMe(int xVal, int yVal) {
x = xVal;
y = yVal;
}
[ October 22, 2008: Message edited by: dev sri ]
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for using equal method use have to have 2 different object of SaveMe .
eg a and b are 2 different object of SaveMe.Then if you use a.equals(b)
then test.x & test.y will reffer to (x,y) of b and x,y will reffer to (x,y) of a.Hope it will make you clear this issue
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah subhasish !! clear thank you !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic