> One returns an integer that represents the hash code
> of the object being queried, the other compares a
> supplied object to the object being queried.
>
> They're implemented in whatever way the class in
> question had them implemented
public class Ex
{
public static void main(String[] s)
{
Ex2 aa = new Ex2();
aa.a = 10;
ExA ab = new ExA();
aa.test();
ab.test();
ab.a = 20;
//ab = aa;
// ab = new Ex2();
System.err.println("aa hashCode: " + aa.hashCode());
System.err.println("ab hashCode: " + ab.hashCode());
if(aa.equals(ab))
{
System.out.println("Both aa & ab r equal...");
}
if(aa.hashCode() == ab.hashCode())
{
System.err.println("Both aa.hashCode() & ab.hashCode() aresame");
}
}
here i ve 3 classes Ex, Ex2, ExA. Ex2 extends ExA.
in these two classes somewhere i ve corresponding variables and methods that are used.
Now if I execute, both the if blocks wont work.
but if I uncomment the 1st comment, then both if blocks executes.
then wer comes the difference b/w equals() and hashCode()in the implementation...
Both if blocks work perfectly fine in either case. In the first, they evaluate the expression to be false and do nothing, which means it works like it should.
Uncommenting the first commented line makes ab hold the same exact object as aa. So of course it evaluates both if statements to be true. They are the same object.
> Uncommenting the first commented line makes ab hold
> the same exact object as aa. So of course it
> evaluates both if statements to be true. They are
> the same object.
yes boss, i understood that they both hold the same mem loc of the object.
but what am not getting s;
1) the diff b/w these two methods.
2) where comes the difference?
> > Uncommenting the first commented line makes ab
> hold
> > the same exact object as aa. So of course it
> > evaluates both if statements to be true. They are
> > the same object.
>
> yes boss, i understood that they both hold the same
> mem loc of the object.
> but what am not getting s;
> 1) the diff b/w these two methods.
You've aleady been told that!
> 2) where comes the difference?
What is the difference between this question and the previous one?
What is it you're having trouble understanding? The two methods have very different signatures, and whether you know what a hash code is or not, equals is pretty self-explanatory
obj1.equals(obj2)
testing if obj1 equal obj2
equal mean : meaningfully equality
hashCode
method return integer supposed to identify for every object ,
whenever you override equals you must override hashCode
if equals method true for two objects then hashCode MUST be the same
> hashCode
>
> method return integer supposed to identify for every
> object ,
Not really. "Identify" makes it sound like each object's hashcode will be unique, when clearly it won't be. In fact return 42; is a perfectly legal hashCode implementation.
> you're right
>
> I was talking about good overriding
I know. My quibble was simply with the word "identify." I don't know if you meant it that way, but it sounds like you were saying that a hashcode would uniquely identify an object. I wanted to make sure the OP or anybody else reading this thread didn't think that was the case.