Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hashcode and equals() coding 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 "Hashcode and equals() coding" Watch "Hashcode and equals() coding" New topic
Author

Hashcode and equals() coding

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi,
I am preparing for SCJP exam.So,the mock exam question is

Suppose that method m1 is invoked with eight instances of the same class and the output is ABCD.

My doubt
---------
How come the output ABCD?My understanding is
For all references(a,b,c,d,e,f,g,h) creted instance is new B().So if we compare anyinstance.equals(anyinstance) that return true as the object references has equal contents.So
if (a.equals(b)) {System.out.print("A");}--->true
if (!c.equals(d)) {System.out.print("B");}--->false
if (e.hashCode() == f.hashCode()) {System.out.print("C");}---->true
if (g.hashCode() != h.hashCode()) {System.out.print("D");}--->false

So my output is AC.Please help me to understand hashcodes.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Shiva Mohan:
...My understanding is For all references(a,b,c,d,e,f,g,h) creted instance is new B().So if we compare anyinstance.equals(anyinstance) that return true as the object references has equal contents...

That seems like an assumption. From what's given above, we don't know how these instances of B were created (what constructor was called), and we don't know how these instances might have been modified before being passed to the method.

Is there more to this question? It seems incomplete.

Since this is for the SCJP exam, I'll move this topic to that forum for you. Please continue this discussion there.
[ August 09, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Sorry for the incomplete question.The given question is

class A {
static void m1 (B a, B b, B c, B d, B e, B f, B g, B h) {
if (a.equals(b)) {System.out.print("A");}
if (!c.equals(d)) {System.out.print("B");}
if (e.hashCode() == f.hashCode()) {System.out.print("C");}
if (g.hashCode() != h.hashCode()) {System.out.print("D");}
}}

Suppose that method m1 is invoked with eight instances of the same class and the output is ABCD. If the B.equals and B.hashCode methods are implemented according to the hash code contract, then which of the following statements must always be true?

a. (a.hashCode() == b.hashCode())
b. (c.hashCode() != d.hashCode())
c. (e.equals(f))
d. (!g.equals(h))


I don't even get the question.Please help me to understand the question.
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
class A {
static void m1 (B a, B b, B c, B d, B e, B f, B g, B h) {
if (a.equals(b)) {System.out.print("A");}
if (!c.equals(d)) {System.out.print("B");}
if (e.hashCode() == f.hashCode()) {System.out.print("C");}
if (g.hashCode() != h.hashCode()) {System.out.print("D");}
}}

Suppose that method m1 is invoked with eight instances of the same class and the output is ABCD. If the B.equals and B.hashCode methods are implemented according to the hash code contract, then which of the following statements must always be true?

a. (a.hashCode() == b.hashCode())
b. (c.hashCode() != d.hashCode())
c. (e.equals(f))
d. (!g.equals(h))

Hi Shiva,
Its already given that the output of the above code is ABCD. What you need to answer is that , in order to get ABCD as output which conditions among the given options must always be true . Let me put it in this way :
1.if (a.equals(b)) {System.out.print("A");}
As A is already printed out it means a.equals(b) is true which again means that a.hashcode()== b.hashcode() mustbe true.So option a must always be true.

2.if (!c.equals(d)) {System.out.print("B");}
The above statement printed B means that (!c.equals(d)) is true.. means that c.equals(d) is false which implies that c.hashcode() may or may not be equal to d.hashcode() ( According to hashcode contract).So option b need not always be true.

3.if (e.hashCode() == f.hashCode()) {System.out.print("C");}
The above statement has printed C which means that e.hashCode() == f.hashCode() , which necessarily does not mean that e.equals(f) must be true. So option c need not be true always.

4.if (g.hashCode() != h.hashCode()) {System.out.print("D");}
If two hashcodes are not equal then g.equals(h) must be false which again needs option d to be true always.

So finally options a and d must be true for the above question .Hope my legthy explanation helps you .
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thankyou very much for the time to write all the options very clearly. I really appreciate it. I have spent the whole day working on this question. It seems we just need to think in a different way.
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Thankyou so much and its good that you understood what i tried to explain !!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that the "hashCode contract" is described in the API documentation under the hashCode() method in java.lang.Object. In particular...
...If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results...

In other words, equal objects must return the same hashCode, but unequal objects are not required to return different hashCodes. That's what's being tested with this question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashcode and equals() coding
 
Similar Threads
Khalid's Question
equals and hashCode
equals, == and boxing/widening
questions about assert
New challenge equals == hashcode(m confused)