热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么hashCode()和getClass()是本地方法?-WhyarehashCode()andgetClass()nativemethods?

IcheckedthesourcecodeofObjectclasswhereIfoundthatmethoddeclarationofgetClass()was我检

I checked the source code of Object class where I found that method declaration of getClass() was

我检查了对象类的源代码,发现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?

为什么这两个方法在类中是本机方法?我如何获得这些方法的源代码?

3 个解决方案

#1


36  

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语言编写的,它不是随源包一起编写的,也不是在Java运行时环境(JRE)的lib位置的rt.jar中编写的。

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来说,这两者都不可能。

#2


31  

Source code for Object class can be found here

对象类的源代码可以在这里找到

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.

由于实际问题,这些方法可能是为性能而设计的。t的实现。

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本机方法的源代码?也可以阅读这个博客文章对象。hashCode实现。它给更多的细节。但是错误地断言hashCode不是由对象的标识生成的。

Hope it helps.

希望它可以帮助。

#3


2  

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.

这些内容的信息在header(类)或其他地方(hashCode)中,这不是可以在Java中实现的。这些方法的源代码位于JVM的源代码中。你可以下载OpenJDK的源代码。


推荐阅读
author-avatar
只属于我一个人的秘密
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有