aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why can not I use toString().length() as a hashCode() return? 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 "Why can not I use toString().length() as a hashCode() return?" Watch "Why can not I use toString().length() as a hashCode() return?" New topic
Author

Why can not I use toString().length() as a hashCode() return?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86



What am I missing? Is there a recurrent call to hashCode() method because of default toString() method?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

The stack trace tells you what methods were called to produce that exception. You didn't post it here, or any part of it, so have a look at it and you should be able to see for yourself.

If you can't figure out what the stack trace is telling you, then post it here (or at least part of it, I expect it's very long) and people will explain it to you.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You might want to have a look at the Javadocs for toString in the Object class. That should make it clearer what's happening.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
There is no implementation in main method. I don't see any exception when I run the program.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

The source code of toString method of Object class, it looks like: -



So, it internally invokes hashCode() method. Now, since you have overrided the hashCode method, it will invoke the hashCode method of your class, which agains invokes toString method of Object class.

This will certainly result in StackOverFlowError

We can rather override the toString method in your class, to make it work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can not I use toString().length() as a hashCode() return?