• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem regarding declarartion of hashcode method in Object class  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Friends,

While going through Object class code, I found a method declaration as
public native int hashCode();


This method seems like abstract method(no definition) but not marked abstract and Object class is also not abstract.

Was wondering, how is this possible.
 
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a method is declared native, that means its implementation is not in the JVM, but rather is on the current platform. This means the implementation may vary between different platforms.
 
saurabh kuma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok ...
Thanks Keith
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means the implementation may vary between different platforms


Are there any chances of getting different hashcode values for same object in different platforms.
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are there any chances of getting different hashcode values for same object in different platforms.



A question is to how much extent different objects in different JVMs (even on different platforms) can be regarded as the same objects.
Some might be considered as the same ("abc", new Integer(123)) whereas others rather not (Object, Socket etc).

In some case the API describes the type-specific way of the computation of the hash code e.g. Integer or String: the hash code of the same String (or of Integer instances representing the same primitive int value) will be the same.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin H. Gupta wrote:Are there any chances of getting different hashcode values for same object in different platforms.

From Object.hashCode()? Yes.

Indeed, it's not even required that hashCode() returns the same hashcode with different invocations of the JVM on the same platform; merely that it returns the same value consistently within the same session. In general, most implementations will return a consistent value based on object content; but it's not guaranteed or required.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!