Previous | Next | Trail Map | Using the JNI | Java Native Interface Programming

Mapping between Java and Native Types

In this section, you will learn how to reference Java programming language types in your native method. You need to reference Java programming language types when you want to:

Java Programming Language Primitive Types

Your native method can directly access Java programming language primitive types such as booleans, integers, floats, and so on, that are passed from programs written in the Java programming language. For example, the Java type boolean maps to the native language type jboolean (represented as unsigned 8 bits), while the Java type float maps to the native language type jfloat (represented by 32 bits). The following table describes the mapping of Java programming language primitive types to native types.

Primitive Types and Native Equivalents
Java Type Native TypeSize in bits
boolean jboolean8, unsigned
byte jbyte8
char jchar16, unsigned
short jshort16
int jint32
long jlong64
float jfloat32
double jdouble64
void voidn/a

Java Programming Language Object Types

Java programming language objects are passed by reference. All references to Java programming language objects have the type jobject. For convenience and to avoid programming errors, the JNI implements a set of types that are conceptually all "subtypes" of jobject, as follows:

  • jobject represents all Java objects.
    • jclass represents Java class objects (java.lang.Class).
    • jstring represents Java strings (java.lang.String).
    • jarray represents Java arrays.
      • jobjectArray represents arrays of objects.
      • jbooleanArray represents boolean arrays.
      • jbyteArray represents byte arrays.
      • jcharArray represents char arrays.
      • jshortArray represents short arrays.
      • jintArray represents int arrays.
      • jlongArray represents long arrays.
      • jfloatArray represents float arrays.
      • jdoubleArray represents double arrays.
    • jthrowable represents Java exceptions (java.lang.Throwable).

In our Prompt.java example, the native method getLine:

private native String getLine(String prompt);
takes a Java programming language string as an argument and returns a Java programming language string. Its corresponding native implementation has type jstring for both the argument and the return value:
JNIEXPORT jstring JNICALL 
Java_Prompt_getLine(JNIEnv *, jobject, jstring);
As mentioned above, jstring corresponds to the Java programming language type String. Notice that the second argument to Java_Promopt_getLine, which is the reference to the object itself, has type jobject.


Previous | Next | Trail Map | Using the JNI | Java Native Interface Programming