The Cygnus Native Interface for C++/Java Integration | ||
---|---|---|
Prev | The Cygnus Native Interface for C++/Java Integration | Next |
While in many ways Java is similar to C and C++, it is quite different in its treatment of arrays. C arrays are based on the idea of pointer arithmetic, which would be incompatible with Java's security requirements. Java arrays are true objects (array types inherit from java.lang.Object). An array-valued variable is one that contains a reference (pointer) to an array object.
Referencing a Java array in C++ code is done using the JArray template, which as defined as follows:
class __JArray : public java::lang::Object { public: int length; }; template<class T> class JArray : public __JArray { T data[0]; public: T& operator[](jint i) { return data[i]; } };
template<class T> T *elements
(JArray<T> &array);extern jintArray foo; jint *intp = elements (foo);The name of this function may change in the future.
There are a number of typedefs which correspond to typedefs from JNI. Each is the type of an array holding objects of the appropriate type:
typedef __JArray *jarray; typedef JArray<jobject> *jobjectArray; typedef JArray<jboolean> *jbooleanArray; typedef JArray<jbyte> *jbyteArray; typedef JArray<jchar> *jcharArray; typedef JArray<jshort> *jshortArray; typedef JArray<jint> *jintArray; typedef JArray<jlong> *jlongArray; typedef JArray<jfloat> *jfloatArray; typedef JArray<jdouble> *jdoubleArray;
You can create an array of objects using this function:
jobjectArray JvNewObjectArray
(jint length, jclass klass, jobject init);For each primitive type there is a function which can be used to create a new array holding that type. The name of the function is of the form `JvNew<Type>Array', where `<Type>' is the name of the primitive type, with its initial letter in upper-case. For instance, `JvNewBooleanArray' can be used to create a new array of booleans. Each such function follows this example:
jbooleanArray JvNewBooleanArray
(jint length);jsize JvGetArrayLength
(jarray array);