Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:13:40+00:00 2026-06-04T09:13:40+00:00

I write a jni function to set the value of two java Integer object

  • 0

I write a jni function to set the value of two java Integer object in C, just like this:

jni_do_something(JNIEnv *env, jobject thiz, jobject p1, jobject p2) {
    jclass c;
    jfieldID id;
    c = env->FindClass("java/lang/Integer");
    if (c==NULL)
    {
        LOGD("FindClass failed");
        return -1;
    }

    id = env->GetFieldID(c, "value", "I");
    if (id==NULL)
    {
        LOGD("GetFiledID failed");
        return -1;
    }

    env->SetIntField(p1, id, 5);
    env->SetIntField(p2, id, 10);
    return 0;
}

In java, I called this function:

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);

The output are both ’10’, why?

===============================================

I have done many tests, and got the following points. (Answer, comments are welcome)

  1. I don’t think this jni native is unable to alter immutable object. After all, the both objects are changed to 10 from 0.

  2. 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:

    Integer p1=0, p2=0;

If change to:

Integer p1=0, p2=1;

or

Integer p1 = new Integer(0);
Integer p2 = new Integer(0);

The result is right (p1:5, p2:10). Can anyone clarify this?

Maybe what I said is not correct. I have write the following code in java:

Integer a = 0;
Integer b = 0;
b = 10;
Log.d("test", "a: "+a);
Log.d("test", "b: "+b);

It output 0, 10. So, they point to not a single object. I’m really confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:13:41+00:00Added an answer on June 4, 2026 at 9:13 am
    Integer i1 = 500, i2 = 10000;
    testInts(i1, i2);
    Log.d("log", "I1 = " + i1); 
    Log.d("log", "I2 = " + i2);
    

    or

    Integer i1 = new Integer(0), i2 = new Integer(0);
    testInts(i1, i2);
    Log.d("log", "I1 = " + i1); 
    Log.d("log", "I2 = " + i2);
    

    output

    I1 = 10
    I2 = 10
    I1 = 5
    I2 = 10
    I1 = 5
    I2 = 10
    

    Works (first result using your example). I have remembered something that if value is in byte (or?) range then java uses some weird optimization.
    EDIT: appears it is a rule for autoboxing for small values.

    c code

        jclass clazz = (*env)->GetObjectClass(env, i1);
        jfieldID mi = (*env)->GetFieldID(env, clazz, "value", "I");
    
        (*env)->SetIntField(env, i1, mi, 5);
        (*env)->SetIntField(env, i2, mi, 10);
        return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a function lv: cfg -> (blabel -> ide set) , which computes the
I am trying to write a JNI applications that uses a dll which is
I was trying to write to an AudioTrack from a jni callback, and I
I'm running a library via JNI (I didn't write it), and internally it calls
write a script that takes two optional boolean arguments,--verbose‚ and ‚--live, and two required
Would it be possible to implement a custom Thread class in Java (using JNI)
I think this is a general Java question, but it does involve some Android
I'm going to write my own Python-Java interface. It is compiled as a DLL
I'm trying to write a Java application that manipulates high resolution .wav files. I'm
I am trying to write a Java applet that will read from a user's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.