Java Reference
In-Depth Information
is no way to obtainaCversion of the entire object array or even a sub-region of
the object array. A language like C or even C
has no idea what the objects
that make up the elements of a Java object array look like or how they behave.
Any knowledge of the structure and behavior of Java objects must be written into
the native code by the programmer.
Individual array elements from an object array can be accessed and modified
with the GetObjectArrayElement() and SetObjectArrayElement()
functions. The return from a Get is a jobject , and the input to a Set
is a jobject of the proper type. For example, for an object array named
the - jo - array ,wecan obtain element number 7 with
++
jsize index = 7;
jobject jo = jenv->GetObjectArrayElement (the - jo - array, 7);
This method throws an ArrayIndexOutOfBoundsException if the index
specified is out of bounds. The return is a jobject , aCreference to a single
instance of one of the Java objects in the Java-side object array. To set a jobject
into an object array, use
jenv- > SetObjectArrayElement (the - jo - array,
index, the - jo - value);
where the - jo - value is the jobject being inserted into the - jo - array .
Before discussing more about jobject s and jobject arrays, we need to
point out that multidimensional primitive arrays in Java are not really prim-
itive arrays. They are implemented in Java as arrays of arrays. Since arrays
are really objects, then an array of arrays is really an array of objects - i.e.
an object array. If a Java int[][] array is passed to C/C
, then a jobject-
Array is received. To access the array elements on the C side, one must first
use the GetObjectArrayElement() function to extract one of the under-
lying single-dimensional arrays. That single-dimensional array is returned as a
jobject that then can be cast into a jintArray and manipulated like any other
jintArray .
The Web Course includes the ArrayExample.java and NativeAr-
ray.cpp codes that demonstrate handling of 2D int arrays.
++
22.8 Java objects on the C side
We mentioned earlier that native codes have full access to Java objects. This
section explains how C/C
can deal with Java objects. Obviously, JNI cannot
have a-priori knowledge of any custom objects that a programmer creates, so those
objects cannot possibly be handled the way Java String objects are handled with
a special jstring data type. In theory, JNI could perhaps provide special data
types for all the myriad objects in the Java API, though doing so would make
JNI unnecessarily huge. Except for a few special cases like jstring , jclass ,
++
Search WWH ::




Custom Search