ArticleslgStudy

computer science

Java Native Interface

Java Native Interface is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java Native Interface rather than just read about it. In short: The Java Native Interface (or Native Method Interface) is a foreign function interface designed for non-Java programming frameworks. The JNI enables Java code to call and be called by native code (i.e. any code using the JNIEnv and interfacing through the C application binary interface), native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such…

Key takeaways

  • Java Native Interface belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java Native Interface to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java Native Interface from memory before moving on to harder problems.

Reference excerpt

The Java Native Interface (or Native Method Interface) is a foreign function interface designed for non-Java programming frameworks. The JNI enables Java code to call and be called by native code (i.e. any code using the JNIEnv and interfacing through the C application binary interface), native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly. Java 22 introduces the Foreign Function and Memory API, which can be seen as the successor to Java Native Interface.

Objectives JNI allows native methods to allow for applications to make calls outside of the Java platform, e.g. when the standard Java class library does not support platform-specific features or a certain program library. It is also used to modify an existing application (written in another programming language) to be accessible to Java applications. Many standard library classes depend on JNI to provide functionality to the developer and the user, such as file I/O and audio APIs. Only applications and signed applets may invoke JNI. The JNI framework allows native code to manipulate and consume Java objects. A native method (marked by the keyword native in Java) can create Java objects and inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code. An application that relies on JNI loses the platform portability Java offers (a partial workaround is to write a separate implementation of JNI code for each platform and have Java detect the operating system and load the correct one at runtime). Not only can native code interface with Java, it can also draw on a Java java.awt.Canvas, which is possible with the Java AWT Native Interface, available since J2SE 1.3. JNI also allows direct access to assembly, without a C bridge. Accessing Java applications from assembly is possible in the same way. The JNI is the entry point between languages used to begin the JVM startup.

Mapping types Java types are the default primitive types available on Java, while JVM type signatures are identifiers used by the JVM to identify the type matching on Java, while native types are types defined by the JNI itself to map to native types. For primitives, the following typedefs are provided:

In addition, the JVM type signature Lfully-qualified-class; refers the class uniquely specified by that name, for example the signature Ljava.lang.String; (Ljava/lang/String; in class files) refers to the class java.lang.String. Prefixing [ to the signature denotes an array of that type; for example, [I refers to int[], and [[I refers to int[][], up to a maximum of 255 dimensions. These types are interchangeable. One can use jint where you normally use an int, and vice versa, without any typecasting required. However, mapping between Java strings and arrays to native strings and arrays is different; for instance, jstring and char* are not interchangeable types, nor are jintArray and (C/C++) int[]. JNI additionally contains the following declarations for Java reference types in <jni.h>:

Here, all Java reference types are defined as opaque empty types which act as handles to Java objects. These types should not be dereferenced.

Additionally, the following opaque types are used as handles:

Note however that jfieldID and jmethodID are just opaque handles, and do not correspond to java.lang.reflect.Field/java.lang.reflect.Method. In GCC's Compiled Native Interface, which allows Java code to interface directly into C++, rather than using opaque handles the Java reference types are translated directly as pointers to those Java objects.

Design The communication between native code and Java code is intermediated by the JNI, acting as an ABI.

JNIEnv is used to access the structure of functions (array of function pointers), and each function being identifiable by a unique pointer. According to the programming language, C or C++, the implementation can be a little different. For example, in C:

Implementation In the JNI framework, native functions are implemented in separate .c or .cpp files (C++ provides a slightly simpler interface with JNI). When the JVM invokes the function, it passes a JNIEnv*, a jobject, and any Java arguments declared by the Java method. For example, the following converts jstring to a const char*:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Java Native Interface

Start with the simplest possible case. Write down what Java Native Interface claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java Native Interface before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java Native Interface ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java Native Interface

In research
Java Native Interface appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java Native Interface in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java Native Interface is common in secondary-school and first-year university syllabi. It links to neighbouring topics Java (programming language), Java (software platform), so understanding it makes those chapters shorter.
In everyday life
Look for Java Native Interface outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Java Native Interface” →

How to study Java Native Interface in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java Native Interface means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java Native Interface out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java Native Interface in simple terms?

The Java Native Interface (or Native Method Interface) is a foreign function interface designed for non-Java programming frameworks. The JNI enables Java code to call and be called by native code (i.e. any code using the JNIEnv and interfacing through the C application binary interface), native app…

Why does Java Native Interface matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java Native Interface?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java Native Interface.

Tags

  • Java (programming language)
  • Java (software platform)

Keep exploring