recipe № 012

passing primitives array from Java to C

In this sample, we will pass all primitive types from Java to C. This time, we will pass them inside arrays. Whenever you pass array of primitives from Java to C, you have to retrieve it’s content using C baed types.

To get the content of the array, you have to use one of the following functions:

Whenever you are done with the computations, you can release the body of array calling one of the corresponding functions:

Typically, schema of the call (when you pass primitives inside array) is very similar in all the cases:

Later on, you can read the explanation for all the primitive types.

After you finish this sample, you should came up with structure similar to one below:

First of all, we need a Java class that will call native code. In this case, library will contain few native functions. Each function will print given inputs. All of the native functions will be packed inside single library file.

As you can see, we are passing each array into separate function. All arrays contain only primitive types.

Again, we have to compile the class and create native’s code header.

After this call, header is created

Note, that each function has a different parameter’s type. You can find there: jdoubleArray, jfloatArray, jlongArray, jintArray, jshortArray, jcharArray, jbyteArray, jbooleanArray.

Now, we can generate implementation of the functions. Pay attention to the code.

Compilation is plain and simple

After all elements are in place, all we have to do is to run the sample

As you can see, Java code produced on the stdout string that was declared inside C.