Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a native implementation in Java? [duplicate]

If we look at the Java Object class then we can find some of the methods like:

public native int hashCode() protected native Object clone() 

What are these natives and how do these methods work?

like image 394
dinsim Avatar asked Feb 17 '09 16:02

dinsim


People also ask

What is native implementation Java?

Native methods are Java™ methods that start in a language other than Java. Native methods can access system-specific functions and APIs that are not available directly in Java. The use of native methods limits the portability of an application, because it involves system-specific code.

What is native implementation?

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).

What is native method stack in Java?

Native Method Stacks. An implementation of the Java Virtual Machine may use conventional stacks, colloquially called "C stacks," to support native methods (methods written in a language other than the Java programming language).


2 Answers

Most native methods are implemented using JNI as mentioned in other answers.

However, performance critical methods such as Object.hashCode are 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.

Many people will claim that Object.hashCode will 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.

like image 20
Tom Hawtin - tackline Avatar answered Oct 19 '22 02:10

Tom Hawtin - tackline


These methods are either Intrinsic or written outside Java in "native" code, that is, specific to the given machine.

The ones you mention are Intrinsic and 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.

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.

like image 71
Nick Fortescue Avatar answered Oct 19 '22 03:10

Nick Fortescue