什么是 Java 中的本机实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/557574/
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
What is a native implementation in Java?
提问by dinsim
If we look at the Java Object class then we can find some of the methods like:
如果我们查看 Java Object 类,那么我们可以找到一些方法,例如:
public native int hashCode()
protected native Object clone()
What are these natives and how do these methods work?
这些本地人是什么?这些方法是如何工作的?
回答by Nick Fortescue
These methods are either Intrinsicor written outside Java in "native" code, that is, specific to the given machine.
这些方法要么是内在的,要么是在 Java 之外以“本机”代码编写的,即特定于给定的机器。
The ones you mention are Intrinsicand part of the JDK but you can also write native methods yourself using the Java Native Interface(JNI). This would normally use C to write the methods, but a lot of other languages, such as python allow you to write methods this way fairly easily. Code is written this way either for performance, or because it needs to access platform specific infrastructure which cannot be done in plain java.
您提到的那些是内在的并且是 JDK 的一部分,但您也可以使用Java 本机接口(JNI)自己编写本机方法。这通常会使用 C 来编写方法,但是很多其他语言(例如 python)允许您很容易地以这种方式编写方法。以这种方式编写代码要么是为了性能,要么是因为它需要访问平台特定的基础设施,而这在普通 Java 中是无法完成的。
In the case of hashcode(), this is implemented by the JVM. This is because often the hashcode will be related to something only the JVM knows. On early JVMs this was related to the object's location in memory - on other JVMs the Object may move in memory, and so a more complicated (but still very fast) scheme may be used.
在 的情况下hashcode(),这是由 JVM 实现的。这是因为哈希码通常与只有 JVM 知道的东西相关。在早期的 JVM 上,这与对象在内存中的位置有关 - 在其他 JVM 上,对象可能会在内存中移动,因此可以使用更复杂(但仍然非常快)的方案。
回答by Tom Hawtin - tackline
Most native methods are implemented using JNI as mentioned in other answers.
如其他答案中所述,大多数本机方法是使用 JNI 实现的。
However, performance critical methods such as Object.hashCodeare typically implemented as intrinsics. When the byte code is compiled into machine code, the Java compiler recognises the method call and inlines appropriate code directly. This is obviously going to be much faster than going through JNI for a trivial method.
但是,诸如此类的性能关键方法Object.hashCode通常作为内在函数实现。当字节码被编译成机器码时,Java 编译器识别方法调用并直接内联适当的代码。这显然比通过 JNI 来实现一个简单的方法要快得多。
Many people will claim that Object.hashCodewill return the address of the object representation in memory. In modern implementations objects actually move within memory. Instead an area of the object header is used to store the value, which may be lazily derived from the memory address at the time that the value is first requested.
许多人会声称Object.hashCode将返回内存中对象表示的地址。在现代实现中,对象实际上在内存中移动。取而代之的是,对象头的一个区域用于存储该值,该值可能是在第一次请求该值时从内存地址延迟导出的。
回答by starblue
Native methods are implemented mostly in C and compiled to native code which runs directly on the machine. This is in contrast to normal methods, which are implemented in Java and compiled to Java byte code, which is executed by the Java Virtual Machine (JVM).
本机方法主要用 C 实现并编译为直接在机器上运行的本机代码。这与普通方法形成对比,普通方法在 Java 中实现并编译为 Java 字节码,由 Java 虚拟机 (JVM) 执行。
To interface to these methods from Java you need to use the Java Native Interface (JNI).
要从 Java 连接到这些方法,您需要使用Java Native Interface (JNI)。
Native code is mostly needed for accessing low-level stuff. In the case of hashCodethis is the address of the object in memory. My guess for cloneis that it copies the raw memory from a give object to the cloned one. Other uses of native code are for access to OS features or hardware.
访问低级内容主要需要本机代码。在hashCode的情况下,这是对象在内存中的地址。我对克隆的猜测是它将原始内存从给定对象复制到克隆对象。本机代码的其他用途是访问操作系统功能或硬件。
The drawback of using native code is that you lose the safety and security of the JVM, i.e. your program might crash or have security holes due to bugs in the native code.
使用本机代码的缺点是您失去了 JVM 的安全性和安全性,即您的程序可能会因本机代码中的错误而崩溃或存在安全漏洞。
回答by Sunlight
Native methods in Java are implemented using the 'Java Native Interface', known as JNI.
Java 中的本机方法是使用“ Java 本机接口”(称为 JNI)实现的。

