File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a doubt in hashcode program 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 "a doubt in hashcode program" Watch "a doubt in hashcode program" New topic
Author

a doubt in hashcode program

sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
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 ]

"SCJP5 | SCWCD5| DEVELOPER"
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
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


Thanks,<br />Subhasish
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
yeah subhasish !! clear thank you !!
 
jQuery in Action, 2nd edition
 
subject: a doubt in hashcode program
 
Similar Threads
program needs to be explained
Static variables are never saved during Serialization, yet my code seems to do the opposite
HashMap Doubt
equals() and hascode() doubt
this.moofvalue