The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Java Native Interface
Lesson: Integrating Java and Native Programs

Mapping Between Java and Native Types

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

Java Primitive Types

Your native method can directly access Java primitive types such as booleans, integers, floats, and so on, that are passed from programs written in Java. 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 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 Object Types

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


This figure has been reduced to fit on the page.
Click the image to view it at its natural size.

In our Prompt.java example, the native method getLine takes a Java string as an argument and returns a Java string:

private native String getLine(String prompt);
Its corresponding native implementation has type jstring for both the argument and the return value:
JNIEXPORT jstring JNICALL
    Java_Prompt_getLine(JNIEnv *, jobject, jstring);

Graphically, this looks as follows:


This figure has been reduced to fit on the page.
Click the image to view it at its natural size.

As mentioned above, jstring corresponds to the Java type String. Notice that the second argument to Java_Prompt_getLine, which is the reference to the object itself, has type jobject.
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.