java 为什么 hashCode() 和 getClass() 是本机方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are hashCode() and getClass() native methods?
提问by krishnakumarp
I checked the source code of Object
class where I found that method declaration of getClass()
was
我检查了Object
类的源代码,发现方法声明getClass()
是
public final native Class<?> getClass();
And the declaration of hashCode()
was
并且声明hashCode()
是
public native int hashCode();
Why are these two methods native
methods in the class and how can I get the source code of those methods?
为什么这两个方法native
是类中的方法,如何获取这些方法的源代码?
回答by Bhavik Ambani
You can find the complete source code of the native methods here
您可以在此处找到本机方法的完整源代码
I hope this will work for you.
我希望这对你有用。
These are native methods, because it has to interact with the machine. Here machine dependent code is written in the C language, which is not coming with the source package or in rt.jar
of the lib
location of the Java Runtime Environment(JRE).
这些是本地方法,因为它必须与机器交互。这里机器相关的代码被写入在C语言中,这是不与源包或在未来rt.jar
的的lib
所述的位置Java运行时环境(JRE)。
One more reason for being native is possibly for the performance reasons. Due to the C level programming performance may be improved, hence they may have written the native code in the C language.
本地化的另一个原因可能是出于性能原因。由于C级编程性能可能会有所提高,因此他们可能已经用C语言编写了本机代码。
The methods are native because they concern native data. The hashCode
method returns an integer value dependent on the internal representation of a pointer to an object on the heap. The getClass
method must access the internal vtbl
(virtual function table) that represents the compiled program's class hierarchy. Neither of these is possible with core Java.
这些方法是本机的,因为它们涉及本机数据。该hashCode
方法返回一个整数值,该值取决于指向堆上对象的指针的内部表示。该getClass
方法必须访问表示已编译程序的类层次结构的内部vtbl
(虚函数表)。对于核心 Java,这些都不可能。
回答by krishnakumarp
Source code for Object class can be found here
可以在此处找到 Object 类的源代码
This source contains implementation of getClass() method (See line 58). hashCode is defined as a function pointer JVM_IHashCode (See line 43).
此源包含 getClass() 方法的实现(参见第 58 行)。hashCode 被定义为一个函数指针 JVM_IHashCode(见第 43 行)。
JVM_IHashCode is defined in jvm.cpp. See code starting from line 504. This in turn calls ObjectSynchronizer::FastHashCode which is defined in synchronizer.cpp. See implementation of FastHashCode at line 576 and get_next_hash at line 530.
JVM_IHashCode 在jvm.cpp 中定义。见代码依次从线504这开始调用它在定义ObjectSynchronizer :: FastHashCode synchronizer.cpp。请参阅第 576 行的 FastHashCode 和第 530 行的 get_next_hash 的实现。
Probably, the methods are native for performance and due to practical issues w.r.t implementation.
可能这些方法对于性能来说是原生的,并且由于实现的实际问题。
For e.g., From javadocs, hashCode is typically implemented "by converting the internal address of the object into an integer". This internal address is not available via java sdk and will have to be implemented as a native method.
例如,在 javadocs 中,hashCode 通常是“通过将对象的内部地址转换为整数来实现的”。此内部地址不能通过 java sdk 获得,必须作为本机方法实现。
Please read Is it possible to find the source for a Java native method?. Also read this blog post Object.hashCode implementation. It gives more details. But makes a wrong assertion that hashCode is not generated from object's identity.
请阅读是否可以找到 Java 本机方法的源代码?. 另请阅读这篇博客文章Object.hashCode implementation。它提供了更多细节。但是错误地断言 hashCode 不是从对象的身份生成的。
Hope it helps.
希望能帮助到你。
回答by Peter Lawrey
The information for these is in the header (for the class) or elsewhere (for the hashCode) This is not something you can implement in Java. The source for these methods is in the source for the JVM. e.g. you can download the source for OpenJDK.
这些信息在头文件中(对于类)或其他地方(对于 hashCode) 这不是您可以在 Java 中实现的。这些方法的源代码位于 JVM 的源代码中。例如,您可以下载 OpenJDK 的源代码。