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 arguments in the method passed to the C + + local function? What is the conversion between the parameters in a Java method and the parameters in a C + + function? I guess you should also have some doubts about it, let's take a look at an example, or use 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, "test", obj, NE W MyClass (), new int[] {});} static {system.loadlibrary ("HelloWorld");}}

A test native method is defined in Helloworld.java, a total of 12 parameters, of which the first 8 are the base data type, and the next 4 are all reference types.


Helloworld.class generated header file contents:

/* Class:     com_study_jnilearn_helloworld * Method:    Test * Signature: (sijfdczbljava/lang/string; Ljava/lang/object;) V */jniexport void Jnicall java_com_study_jnilearn_helloworld_test  (jnienv *, Jclass, Jshort, Jint, Jlong, Jfloat, jdouble, Jchar, Jboolean, Jbyte, jstring, Jobject, jobject);

The prototype of the file function from the beginning can be learned that the data type of the parameter in the test method is automatically converted to the corresponding data type in the JNI, it is not difficult to understand that when the Java native method is called to pass the arguments to the C + + function, the data type of the Java parameter is automatically converted to C + + Corresponding data types, so we must understand the correspondence between the data types when we write the JNI program.

data types in the Java language are divided into basic data types and reference types, where 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 correspond to Jbyte, Jchar, Jshort, Jint, Jlong, Jfloat, Jdouble, Jboolean in the JNI data type, respectively. Reference types are all represented by Jobject in Jni, and Jobject has many subclasses: Jstring, Jclass, jthrowable, Jarray, where Jarray has 8 subclasses, respectively, corresponding to 8 basic data types in Java (Jintarray , Jshortarray, Jlongarray, etc.), string types in Java are represented by jstring in JNI because they are often used in development. Now look back at the head to see 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);

As can be seen from the parameters of the above two functions, except for 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 documentation:

Basic data type:



Reference type:


Attention:

1, JNI if written in the C + + language, all reference types derive from Jobject, using the inheritance structure attributes of C + +, using the appropriate type. As shown below:

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

2, JNI if written in C, all reference types use Jobject, and other reference types are redefined using TypeDef, 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 any type of data declared in the Union. In JNI, the basic data type and reference type are defined in a union type, representing variables defined with Jvalue, which can store data of any JNI type, followed by the application of Jvalue in JNI programming. 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 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

Related Article

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.