How JNI Programming passes parameters (ii)--Transmission of array types __ Programming

Source: Internet
Author: User
Tags documentation int size

As with string, JNI provides a j*array type for an array of Java primitive types, such as int[] corresponding to Jintarray. To see an example of an array of int, the Java program is not written,

Jniexport jintjnicall Java_intarray_sumarray (jnienv *env, jobject obj, Jintarray arr)
{jint *carr
   ;
   Carr = Env->getintarrayelements (arr, false);
   if (Carr = NULL) {return
       0;/* Exception occurred */
   }
   Jint sum = 0;
   for (int i=0; i<10; i++) {
       sum = Carr[i];
   }
   Env->releaseintarrayelements (arr, Carr, 0);
   return sum;
}


The getintarrayelements and releaseintarrayelements functions in this example are JNI, which provides functions for dealing with arrays of Int. If you try to access the Jintarray type in a arr[i way, there is no doubt that there will be an error. JNI also provides another pair of functions getintarrayregion and releaseintarrayregion access to an int array, which is not described, for other basic types of arrays, the method is similar.

Two-dimensional arrays and string arrays

In Jni, both two-dimensional and string arrays are treated as object arrays, because arrays and strings are treated as object. Still with an example to illustrate, this time is a two-dimensional int array, as the return value.

Jniexportjobjectarray jnicall Java_objectarraytest_initint2darray (jnienv *env, jclasscls, int size)
{
   Jobjectarray result;
   Jclass intarrcls = Env->findclass ("[I");
   result = Env->newobjectarray (size, intarrcls, NULL);
   for (int i = 0; i < size; i++) {
       jint tmp[256];/* Make sure it is large enough!/jintarray iarr
       = Env->ne Wintarray (size);
       for (int j = 0; J < size; J +) {
           Tmp[j] = i + j;
       }
       Env->setintarrayregion (Iarr, 0, size, tmp);
       Env->setobjectarrayelement (result, I, Iarr);
       Env->deletelocalref (Iarr);
   }
   return result;
}


The third line in the code above,
Jobjectarray result;
Because you want to return a value, you need to create a new Jobjectarray object.

Jclass intarrcls = Env->findclass ("[I");
is to create a jclass reference, because the element of result is a reference to a one-dimensional int array, so the intarrcls must be a reference to the one-dimensional int array, how is this guaranteed? Note the findclass parameter "[i", which is used by JNI to determine the type of reference, and I represents the type int, [identity is an array.] For other types, there is a corresponding representation method,

Z Boolean
B byte
C Char
S Short
I int
J Long
F float
D Double

String is through "ljava/lang/string;" represented, the corresponding String array should be "[ljava/lang/string;"].

Or back to the code,
result = Env->newobjectarray (size, intarrcls, NULL);
The function is to allocate space for result.

Jintarray Iarr = env->newintarray (size);
is to allocate space for a one-dimensional int array Iarr.

Env->setintarrayregion (Iarr, 0, size, TMP);
is to assign a value to the Iarr.

Env->setobjectarrayelement (result, I, Iarr);
is to assign a value to the first element of result.

With these steps, we create a two-dimensional int array and assign the value so that we can return it as a parameter.

If you understand the content described above, basically most of the tasks can be dealt with. While manipulating array types, especially two-dimensional arrays and string arrays, is more cumbersome than programming in a separate language, since we enjoy the benefits of Cross-language programming, there must be a price to pay.

One thing to add is that the function calls used in this article are for C + +, if you want to use in C, all the env-> will be replaced by (*ENV), and the following function needs to add a parameter env, please look at the jni.h code. There are also some omitted content that can refer to the JNI documentation: Java Native Interface 6.0 specification, which can be found in the JDK documentation. If you want to do more in-depth JNI programming, you need to read this document carefully

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.