0

I originally posted here (include the code): Weird: can not change the value of Integer in jni

To pass multiple value from jni to java, I pass 2 Integer reference to jni/c, and use SetIntField() to modify the object.

Unfortunately, the 2 value are the same. Lawrence D'Oliveiro explains Integer is not mutable, so we cant change it. I don't know why.

As a workaround, I created my own Integer class:

public class MyInteger {
    int value;

    public MyInteger(int v) {
        value = v;
    }
}

And pass object of this type to jni:

native int do_something(MyInteger p1, MyInteger p2);

It works. I'm curious why Integer doesn't work. Thanks all.

Community
  • 1
  • 1
pengguang001
  • 4,045
  • 6
  • 27
  • 31

1 Answers1

0

This issue has no relation with 'immutable object'. The original code using Integer doesn't work is because of autoboxing.

pengguang001
  • 4,045
  • 6
  • 27
  • 31