diff b/w of equals() n hashCode()

hi friends,can anybody help me tracing out the difference b/w equals() and hashCode() in java. how they r implemented?
[132 byte] By [Shaik_Sajana] at [2007-11-27 5:24:03]
# 1
The jdk comes with the source code for all the api classes. There's a zip file called src under your jdk install directory.
hunter9000a at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 2
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
georgemca at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 3

> 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...

Shaik_Sajana at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 4

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.

bsampieria at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 5

> 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?

Shaik_Sajana at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 6

> > 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

georgemca at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 7
Did you read the docs for the two methods? It should be clear from that. You can also read the relevant parts of this: http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
jverda at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 8

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

eaajea at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 9

> 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.

jverda at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 10
you're right I was talking about good overriding
eaajea at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 11
> you're right > > I was talking about good overridingJeff's point was, hash codes are very specifically not a unique identifier for an object
georgemca at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...
# 12

> 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.

jverda at 2007-7-12 11:50:14 > top of Java-index,Java Essentials,New To Java...