• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

hashcode question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please anyone help me on this desperate hashcode question....
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could anyone please help me on this..
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic