Search code examples
javaandroidc++java-native-interfacesignature

JNI why jintArray cannot be considered as [I instead of Ljava/lang/Object?


I'm curious about the following code snippet:

jintArray someJIntArray;
jfloatArray someJFloatArray;

// ... put legit values to someJIntArray and someJFloatArray

jclass pairClass = env->FindClass("android/util/Pair");

// #1
jmethodID pairConstructor = env->GetMethodID(pairClass, "<init>", "(Ljava/lang/Object;Ljava/lang/Object;)V");

// #2
jmethodID pairConstructor = env->GetMethodID(pairClass, "<init>", "([I[F)V");

jobject pairObject = env->NewObject(pairClass, pairConstructor, someJIntArray, someJFloatArray);

If I use #1 , then my JNI code runs okay. But if I use #2 then Pending exception java.lang.NoSuchMethodError: no non-static method "Landroid/util/Pair;.<init>([I[F)V" error occurs.

I wonder, why I cannot use [I and [F for jintArray and jfloatArray? Actually I'm also curious why Ljava/lang/Object; works in this code snippet while int and float are not objects in Java.


Solution

  • You are specifically asking for it to find the constructor Pair(int[], float[]), which doesn't exist.

    It's not about the value you are going to pass in, but about the types of the formal parameters.