JNI/NDK Development Guide (iii)--JNI data types and mapping to Java data types

Source: Internet
Author: User


Reprint Please specify Source: http://blog.csdn.net/xyang81/article/details/42047899


When we call a Java native method. How are the parameters in the method passed to the C + + local function? The parameters in the Java method and the number of references in C + + functions. How do they convert between them? I guess you should have some doubts about that, too. Let's look at a sample first. 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);p ublic static void Main (Strin G[] 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, which has 12 parameters, the first 8 of which are the basic data types. All 4 of the following are reference types.



Prototype and implementation of native function 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);}

The output of the test method is called:


From the beginning of the prototype of the file function can be learned that the test method of the shape of the data type itself into the corresponding data type in JNI, it is not difficult to understand, when calling the Java native method will be passed to the C + + function, the Java-shaped data type of its own initiative to convert to C /c++ the corresponding data type, so we are writing the JNI program. It is necessary to identify the correspondence between their data types.

The data types in the Java language are divided into basic data types and reference types. There are 8 basic data types: Byte, char, short, int, long, float, double, Boolean. Other than the base data type are reference types: Object, String, array, and so on. The 8 basic data types are Jbyte, Jchar, Jshort, Jint, Jlong, Jfloat, jdouble, Jboolean, respectively, in the JNI data type.

All JNI reference types are all jobject types, and for ease of use and type safety, JNI defines a collection of reference types. All of the types in the collection are subclasses of Jobject, and these subclasses correspond to the reference types that are frequently used 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 8 subclasses, respectively, corresponding to 8 basic data types in Java (Jintarray, Jshortarray, Jlongarray, etc.). Here's a look at the corresponding relationship between the test method and the parameter type 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, Str ing 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);

it can be seen from the parameters of the above two functions. In addition to the two parameters jnienv and Jclass, the other parameters are one by one corresponding. The following is a description of the corresponding relationship between Java and JNI data types in the JNI specification document:

Basic data type:



Reference type:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehlhbmc4mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Attention:

1. If the JNI hypothesis is written in the C + + language, all reference types are derived from Jobject, using the inheritance structure characteristics of C + +, using the corresponding type.

For example, see the following:

Class _jobject {};   Class _jclass:public _jobject {};   Class _jstring:public _jobject {};   Class _jarray:public _jobject {};   Class _jbooleanarray:public _jarray {};   Class _jbytearray:public _jarray {};   ...

2. The JNI hypothesis is written using C language. All reference types use Jobject, and other reference types are defined using typedef once again, such as typedef jobject jstring


Jvalue Type:

Jvalue is a Unio (union) type. In order to conserve memory in C, a union type variable is used to store arbitrary types of data declared in a union. In JNI, the basic data type and reference type are defined in a union type, representing variables defined with Jvalue, capable of storing data of arbitrary jni types, followed by the application of Jvalue in JNI programming. Prototypes such as the following:

typedef Union JVALUE {    Jboolean z;    Jbyte    B;    Jchar    C;    Jshort   S;    Jint     i;    Jlong    J;    Jfloat   F;    Jdouble  D;    Jobject  l;} jvalue;
If the Unio type is not very clear to the students, please refer to the relevant information, not in detail here.



JNI/NDK Development Guide (iii)--JNI data types and mapping to Java data types

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.