JNI Part 3: Passing Arguments and Mapping Types

  1. The implemented JNI native methods have two standard parameters, in addition to the arguments declared in their Java-side declaration.
  2. The first parameter, the JNIEnv interface pointer, points to a location that contains a pointer to a function table.
  3. Each entry in the function table points to a JNI function.
  4. Native methods always access data structures in the Java virtual machine through one of the JNI functions.
  5. The second parameter differs depending on whether the native method is a static or an instance method.
  6. 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++.
  7. The second argument to a static native method is a reference to the class in which the method is defined.
  8. In the previous example, Java_HelloWorld_print() implements an instance native method. Thus the jobject parameter is a reference to the object itself.
JNI Passing arguments

Mapping Types

  1. Argument types in the native method declaration have corresponding types in native programming languages.
  2. The JNI defines a set of C and C++ types that correspond to types in the Java programming language.
  3. There are two kinds of types in the Java programming language:
    1. Primitive types such as int, float, double and char;
    2. Reference types such as classes and arrays.
  4. 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.
  5. 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.
  6. The JNI treats primitive types and reference types differently.
  7. The JNI passes objects to native methods as opaque references.
  8. Opaque references are C pointer types that refer to internal data structures in the Java virtual machine.
  9. The exact layout of the internal data structures, however, is hidden from the programmer.
  10. All JNI references are of or inherit the type jobject.

Primitive types: There are eight primitive types in Java

Type Description
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.

Example:

  1. 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),
  2. 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
boolean jboolean 8, unsigned
byte Jbyte 8
char Jchar 16, unsigned
short Jshort 16
int Jint 32
long Jlong 64
float Jfloat 32
double Jdouble 64
void Void n/a

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

Reference types:

  1. Here Objects are passed by reference
  2. 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.

Example:
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.

Mapping Types

§Argument types in the native method deMapping Types
?    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.
There are eight primitive types in Java:
claration 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 typessuch as int, float, double and char;

§reference typessuch 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.

There are eight primitive types in Java:

Leave a Reply