wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode question" Watch "hashcode question" New topic
Author

hashcode question

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Given this code, which of the following statements are true?

1) The hashCode method is correctly implemented
2) The hashCode method is not correct because it is inconsistant with the equals method
3) The code will not compile because there is a circular reference to the hashCode method
4) The code will not compile because the return value of this.hashCode is not an int value
5) The code will not compile because it does not implement the comparable interface

what is the output since the mock exam didnot give the answer,i am asking you guys.Until now,i have attended mock exam questions in hashcode like "if the equals() method returns false,the hashcode() comparision == might return true".

In order to find the answer of the preceding one,i executed that program like

Those errors at the program only i got.

Also,can anyone clear the meaning of 2 & 3 options for me,please..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

When you made your test program, you changed hashCode() in a small but very significant way; this change is what's causing the stack overflow error. It's rather important that you understand why your change has this effect -- knowing what super.hashCode() means is most definitely important for the exam.

The answer to the question is "1". To understand "2", read the Javadoc for the Object.hashCode() and Object.equals() methods closely. "3" is just gibberish; one doesn't speak of "circular references to methods" in Java. You could interpret it to mean a recursive call, but it's not.


[Jess in Action][AskingGoodQuestions]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks for the reply Ernest.But still i am having trouble on this program.
super.hashcode() will execute Object.hashcode().right?Over there(according to the API) for each different objects,different hashcode will generated.
But when i executed the above program with the mein method



when i commentout line4 upto line6,and the executiong result is

equalsfalse
hashcode23671010
object hashcode17332331
this.hashcode17332331

looks like, (for executing equals(),object.equlas() is executed---->false)
But for hashcode,it executed Objective2425.hashcode (thru that,super.hashcode is executed)

likewise,when i commentout line1 upto line3,and the executiong result is
ss--------true
object hashcode23671010
mn.hashcode23671010(returned hashcode value)
object hashcode17332331
Objectr.hashcode17332331(returned o.hashcode)
object hashcode23671010
object hashcode17332331
equalsfalse
object hashcode23671010
hashcode23671010
object hashcode17332331
this.hashcode17332331

(for executing equals(),Objective2425.equals() is executed---->false
it executed Objective2425.hashcode (thru that,super.hashcode is executed)

But i don't understand how the process and rules are working here.please provide me further help.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Please anyone help me on this desperate hashcode question....
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Your printouts don't come from the program you've shown, because that program doesn't print "object" anywhere. Without seeing exactly the program you're executing, no one can explain how it behaves!
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



when i commentout line4 upto line6,and the executiong result is

equalsfalse
hashcode23671010
object hashcode17332331
this.hashcode17332331

looks like, (for executing equals(),object.equlas() is executed---->false)
But for hashcode,it executed Objective2425.hashcode (thru that,super.hashcode is executed)

likewise,when i commentout line1 upto line3,and the executiong result is
ss--------true
object hashcode17332331
mn.hashcode*********17332331
object hashcode18464898
Objectr.hashcode********818464898
object hashcode17332331
object hashcode18464898
equalsfalse
object hashcode17332331
hashcode17332331
object hashcode18464898
this.hashcode18464898

(for executing equals(),Objective2425.equals() is executed---->false
it executed Objective2425.hashcode (thru that,super.hashcode is executed)

But i don't understand how the process and rules are working here.please provide me further help.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
could anyone please help me on this..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

looks like, (for executing equals(),object.equlas() is executed---->false)
But for hashcode,it executed Objective2425.hashcode (thru that,super.hashcode is executed)


There is no magic here... the compiler will do exactly what you ask. So when you say...



m is a reference to an Object, and it actually refers to an Object instance. So when you call m.equals(), it will call the equals() methods of the m instance.

In your hashcode example, one is your example object, the other is an Object object, and it does one in one case and the other in the other case.

When you get rid of the Object instance later, it does your example in both cases.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Huh.Thank you so much henry.Really hearty thanks to you.Until now, i have doubts on so many things,that is getting gradually understanding by only you guys help.Thanks again for that.

One more doubt on this.Please look at the another program.



s1 is StringBuffer object and s2 is in the Stringpool.I thought s1.equals() called the stringBuffer.equals() like the above program we worked.

//StringBuffer.equals() called--> so shallow comparision --->look for both object references point to the same object.but no here.so false

But it didn't print Object.equals String.Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

But it didn't print Object.equals String.Why?


Not sure what you are asking. Are you saying because you have a class named Object, that StringBuffer should now inherit from your class, instead of the java.lang.Object class in the core libs? Or are you modifying the core libs?

Henry
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Sorry for the cofusion.I haven't noticed that the super class name, i have given is Object.



StringBuffers cannot override equals() method.so Object class equals() method will run. Then what is the use of Object1 overridden equals() method.is there any way to execute line 1 and line 2.
Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Then what is the use of Object1 overridden equals() method.is there any way to execute line 1 and line 2.


In your example, it serves no purpose whatsoever -- as you never instantiated an Object1 (or an Objective2425) object. So you certainly never used the equals() method of the Object1 class.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hashcode question