Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashcode and equals() coding

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou so much and its good that you understood what i tried to explain !!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic