JNI/NDK Development Guide (iii) -- ing between JNI data types and Java data types, ndkjni

Source: Internet
Author: User

JNI/NDK Development Guide (iii) -- ing between JNI data types and Java data types, ndkjni


Reprinted please indicate the source: http://blog.csdn.net/xyang81/article/details/42047899


When we call a Java native method, how does the parameter in the method pass to the local function in C/C ++? How are the parameters in the Java method converted from those in the C/C ++ function? I guess you should have related concerns. Let's take a look at an example, or take HelloWorld as an example:

HelloWorld. java:

Package com. study. jnilearn; class MyClass {} public class HelloWorld {public static native void test (short s, int I, long l, float f, double d, char c, boolean z, byte B, string str, Object obj, MyClass p, int [] arr); public static void main (String [] args) {String obj = "obj"; short s = 1; long l = 20; byte B = 127; test (s, 1, l, 1.0f, 10.5, 'A', true, B, "China", obj, new MyClass (), new int [] {});} static {System. loadLibrary ("HelloWorld ");}}

A test native method is defined in HelloWorld. java. There are a total of 12 parameters in this method. The first eight are basic data types, and the last four are all reference types.


Native function prototype and implementation generated by HelloWorld. class:

/* * Class:     com_study_jnilearn_HelloWorld * Method:    test * Signature: (SIJFDCZBLjava/lang/String;Ljava/lang/Object;Lcom/study/jnilearn/MyClass;[I)V */JNIEXPORT void JNICALL Java_com_study_jnilearn_HelloWorld_test    (JNIEnv *env, jclass cls, jshort s, jint i, jlong l, jfloat f,     jdouble d, jchar c, jboolean z, jbyte b, jstring j_str, jobject jobj1, jobject job2, jintArray j_int_arr){    printf("s=%hd, i=%d, l=%ld, f=%f, d=%lf, c=%c, z=%c, b=%d", s, i, l, f, d, c, z, b);    const char *c_str = NULL;    c_str = (*env)->GetStringUTFChars(env, j_str, NULL);    if (c_str == NULL)    {        return; // memory out    }    (*env)->ReleaseStringUTFChars(env, j_str, c_str);    printf("c_str: %s\n", (char*)c_str);}

Result of calling the test method:


From the prototype of the file function, we can see that the data type of the parameters in the test method is automatically converted to the corresponding data type in JNI, which is not hard to understand, when the Java native method is called to pass real parameters to the C/C ++ function, the Data Types of java parameters are automatically converted to the corresponding data types of C/C ++, therefore, when writing a JNI program, we must understand the relationship between the data types between them.

In Java, data types are classified into basic data types and reference types. The basic data types include byte, char, short, int, long, float, double, and boolean, in addition to the basic data types, all references include Object, String, and array. The eight basic data types correspond to jbyte, jchar, jshort, jint, jlong, jfloat, jdouble, and jboolean in JNI data types. All JNI reference types are of the jobject type. For ease of use and type security, JNI defines a set of reference types. All types in the set are subclasses of jobject, these subclasses correspond to common reference types in Java. For example, jstring represents a string, jclass represents a class bytecode object, jthrowable represents an exception, jarray represents an array, and jarray derives eight subclasses, corresponding to eight basic data types in Java (such as jintArray, jshortArray, and jlongArray ). Next we will review the header to see the correspondence between the test method and the parameter types in the Java_com_study_jnilearn_HelloWorld_test function:

// HelloWorld.javapublic static native void test(short s, int i, long l, float f, double d, char c, boolean z, byte b, String str, Object obj, MyClass p);// HelloWorld.hJNIEXPORT void JNICALL Java_com_study_jnilearn_HelloWorld_test  (JNIEnv *, jclass, jshort, jint, jlong, jfloat, jdouble, jchar, jboolean, jbyte, jstring, jobject, jobject, jintArray);

From the parameters of the above two functions, we can see that, except for the JNIEnv and jclass parameters, other parameters are one-to-one. The following describes the ing between Java and JNI Data Types in the JNI standard document:

Basic Data Type:



Reference Type:


Note:

1. If JNI is written in C ++, all reference types are derived from jobject, and corresponding types are used in the inheritance structure of C ++. As follows:

class _jobject {};   class _jclass : public _jobject {};   class _jstring : public _jobject {};   class _jarray : public _jobject {};   class _jbooleanArray : public _jarray {};   class _jbyteArray : public _jarray {};   ...

2. If JNI is written in C language, jobject is used for all reference types, and typedef is used for redefinition for other reference types, such as typedef jobject jstring.


Jvalue type:

Jvalue is a unio (union) type. To save memory, jvalue uses the Union type variable to store any type of data declared in the consortium. In JNI, the basic data type and reference type are defined in a joint type, which indicates the variables defined with jvalue. Any JNI type data can be stored, the Application of jvalue in JNI programming will be introduced later. The prototype is as follows:

typedef union jvalue {    jboolean z;    jbyte    b;    jchar    c;    jshort   s;    jint     i;    jlong    j;    jfloat   f;    jdouble  d;    jobject  l;} jvalue;
If you are not familiar with the unio type, please refer to the relevant materials.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.