• Post Reply Bookmark Topic Watch Topic
  • New Topic

Negative Hashcode value  RSS feed

 
Charles HAN
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

When i run the method Hashcode() of an instance object in JAVA, it returns a negative value which seems to be odd! Does any body know why? Because i am using Hashcode as Hashvalue in the hashtable.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why shouldn't negative values be allowed? They're just numbers. It doesn't matter if they're negative or not. Allowing them to be negative means that we can have 2^32 possible values using 4-byte data types, rather than 2^31. (Since Java does not generally support the concept of unsigned types as found in other languages like C/C++).

Basically, hashcodes can be negative because there's no reason not to allow them to be negative. I can't really think of a better way to say it, so I hope that helps...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!