• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

General Java doubt

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my program :-



And the output is

in the hashcode
in the hashcode
in the hashcode
v hascode====100
in the hashcode
hhhhhhhh===variable@64

I have a doubt why the "in the hashcode" is printing 4 times it should be printing 2 times that is while s.add(v);s.add(v); only. please anyone clarify my doubt does hashcode method internally calls equals or what please clarify me why "in the hashcode" is printing 4 times.

Thanks in advance

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You should always use the code button; I shall try adding it to your post later, and if it works it will look much better
Inspect the documentation for Object#toString(), and I hope that will show where the other two invocations of hashCode occur.
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

You should always use the code button; I shall try adding it to your post later, and if it works it will look much better
Inspect the documentation for Object#toString(), and I hope that will show where the other two invocations of hashCode occur.




Hi Campbell Ritchie,

Thanks but i did not get the point why the "in the hascode" is being called i have gone through the link which you have given. kindly explain me it would be helpful to me.

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try overriding the toString method, and you will only get two calls. Notice the unoverridden toString method displays the hash code.
 
author
Posts: 23951
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

srikanth darbha wrote:
Hi Campbell Ritchie,

Thanks but i did not get the point why the "in the hascode" is being called i have gone through the link which you have given. kindly explain me it would be helpful to me.

Thanks in advance.




I am interested to know why "in the equals" didn't get printed. After all, you added the same object twice -- this means that the hashset will need to check for duplicates.

Henry
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Try overriding the toString method, and you will only get two calls. Notice the unoverridden toString method displays the hash code.




Thanks Campbell Ritchie.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have now got it:
Two calls to hash code when you enter the object into the set.
One call when you print it out.
One call when you call toString on the one instance in the set.

Henry is right: why does it not print “in equals”? It is possible that method has been optimised away by the return true;?That is part of its bytecode, so no, it has not been optimised away.
 
Henry Wong
author
Posts: 23951
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

Campbell Ritchie wrote:
Henry is right: why does it not print “in equals”? It is possible that method has been optimised away by the return true;?That is part of its bytecode, so no, it has not been optimised away.




Looking at the source code, it looks like the add() method call does an "==" or an "equals". So, if it is the same instance, the equals get short-circuited from running.

Henry
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean; if the == operator is called before equals() you will never see that output.
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I see what you mean; if the == operator is called before equals() you will never see that output.



But as far as i know in the internal implementation of the add() the equals() should be called but why is it not calling.i.e,"in the equals" should be printed? can anyone explain the reason for the code sniplet given above.

Thanks in advance..
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srikanth darbha wrote: . . . i know in the internal implementation . . .

No, you don't. You know nothing about internal implementations, which might change anyway. At least that is the assumption which the programmers made. They know they can use the == test because of the general contract of equals(); it was regarded a not a serious problem that it may be used twice.
You know that it takes equals() as defining equality, because that appears in the documentation.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is possible that method has been optimised away by the return true;?



No, that would not be allowed. The compiler and hotspot can't optimize away anything that has any side effects.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have since thought that the == test might cover the instance when you pass null; finding another null will cause == to return true and the following || will prevent the equals() call being made, and obviate a null exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic