- The implemented JNI native methods have two standard parameters, in addition to the arguments declared in their Java-side declaration.
- The first parameter, the JNIEnv interface pointer, points to a location that contains a pointer to a function table.
- Each entry in the function table points to a JNI function.
- Native methods always access data structures in the Java virtual machine through one of the JNI functions.
- The second parameter differs depending on whether the native method is a static or an instance method.
- The second argument to an instance native method is a reference to the object on which the method is invoked, similar to the ‘this’ pointer in C++.
- The second argument to a static native method is a reference to the class in which the method is defined.
- In the previous example, Java_HelloWorld_print() implements an instance native method. Thus the jobject parameter is a reference to the object itself.
- Argument types in the native method declaration have corresponding types in native programming languages.
- The JNI defines a set of C and C++ types that correspond to types in the Java programming language.
- There are two kinds of types in the Java programming language:
- Primitive types such as int, float, double and char;
- Reference types such as classes and arrays.
- The variables of primitive types contain a single value – a number, character or boolean value. A variable of primitive type has a specified size and format.
- The variables of reference type contain a reference to (an address of) the value or set of values represented by the variable. Arrays, classes and interfaces are reference types.
- The JNI treats primitive types and reference types differently.
- The JNI passes objects to native methods as opaque references.
- Opaque references are C pointer types that refer to internal data structures in the Java virtual machine.
- The exact layout of the internal data structures, however, is hidden from the programmer.
- All JNI references are of or inherit the type jobject.
Primitive types: There are eight primitive types in Java
|boolean||Similar to the C++ bool type but cannot be converted to int. An integer or pointer cannot be used in a boolean context (such as an if condition they way it can in C or C++.|
|char||Similar to the C char type but uses 16 bits.|
|byte||An 8-bit signed integer.|
|short||A 16-bit signed integer.|
|int||A 32-bit signed integer.|
|long||A 64-bit signed integer.|
|float||A 32-bit floating point number.|
|double||A 64-bit floating point number.|
The mapping of primitive types is straightforward.
- The type int in the Java programming language maps to the C/C++ type jint (defined in jni.h as a signed 32-bit integer),
- The type float in the Java programming language maps to the C and C++ type jfloat (defined in jni.h as a 32-bit floating point number).
|Java Type||Native Type||Size in bits|
- Here Objects are passed by reference
- All objects have type jobject as shown in below image
The native code must manipulate the underlying objects via the appropriate JNI functions, which are available through the JNIEnv interface pointer.
The corresponding JNI type for java.lang.String is jstring. The exact value of a jstring reference is irrelevant to the native code. The native code calls JNI functions such as GetStringUTFChars() to access the contents of a string.
Leave a Reply