In our initial JNI example, we declared a Java method that took an int parameter and saw that the resulting C function generated by the javah tool took (along with a couple of other parameters) a parameter of type jint. In fact, the Java method also returned an int, and the corresponding C method a jint. Here are the two side by side as a reminder:

public native static int getDoubled(int n);
JNIEXPORT jint JNICALL Java_test_Test_getDoubled(JNIEnv *,
    jclass, jint);

From this example, you'll hopefully be unsurprised to learn that for each Java primitive type, there is a corresponding C type declared in the JNI headers. So a Java boolean becomes a C jboolean, a Java char a C jchar etc:

Java primitive typeCorresponding C type with JNIBytes
charjchar2 (unsigned)
shortjshort2 (signed)

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.