Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird: can not change the value of Integer in jni
    primarykey
    data
    text
    <p>I write a jni function to set the value of two java Integer object in C, just like this:</p> <pre><code>jni_do_something(JNIEnv *env, jobject thiz, jobject p1, jobject p2) { jclass c; jfieldID id; c = env-&gt;FindClass("java/lang/Integer"); if (c==NULL) { LOGD("FindClass failed"); return -1; } id = env-&gt;GetFieldID(c, "value", "I"); if (id==NULL) { LOGD("GetFiledID failed"); return -1; } env-&gt;SetIntField(p1, id, 5); env-&gt;SetIntField(p2, id, 10); return 0; } </code></pre> <p>In java, I called this function:</p> <pre><code>native int do_something(Integer p1, Integer p2); Integer p1=0, p2=0; do_something(p1, p2); Log.d("test", "p1: "+p1); Log.d("test", "p2: "+p2); </code></pre> <p>The output are both '10', why?</p> <p>===============================================</p> <p>I have done many tests, and got the following points. (Answer, comments are welcome)</p> <ol> <li><p>I don't think this jni native is unable to alter immutable object. After all, the both objects are changed to 10 from 0.</p></li> <li><p>There is some relation to auto-boxing (yes? I'm not sure). Maybe p1 and p2 are specially processed by jvm, made pointed to a single object if initialized with:</p> <p>Integer p1=0, p2=0;</p></li> </ol> <p>If change to:</p> <pre><code>Integer p1=0, p2=1; </code></pre> <p>or</p> <pre><code>Integer p1 = new Integer(0); Integer p2 = new Integer(0); </code></pre> <p>The result is right (p1:5, p2:10). Can anyone clarify this?</p> <p>Maybe what I said is not correct. I have write the following code in java:</p> <pre><code>Integer a = 0; Integer b = 0; b = 10; Log.d("test", "a: "+a); Log.d("test", "b: "+b); </code></pre> <p>It output 0, 10. So, they point to not a single object. I'm really confused.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload