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 7908865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:07:49+00:00 2026-06-03T12:07:49+00:00

// example-1: GetStringUTFChars() public class Prompt { // native method that prints a prompt

  • 0
// example-1: GetStringUTFChars()
public class Prompt
{
    // native method that prints a prompt and reads a line
    private native String getLine(String prompt);
    public static void main(String args[])
    {
        Prompt p = new Prompt();
        String temp = new String("Type a line: ");
        String input = p.getLine(temp);
        System.out.println("User typed: " + input);
        System.out.println(temp);
    }
    static
    {
        System.loadLibrary("Prompt");
    }
}
/*
javac Prompt.java
javah Prompt
get a file: Prompt.h
*/

#include "Prompt.h"
#include <stdio.h>
#include <string.h>
JNIEXPORT jstring JNICALL Java_Prompt_getLine (JNIEnv *env, jobject obj, jstring prompt)
{
    char buf[128];
    const jbyte *str;
    jboolean isCp;
    str = (*env)->GetStringUTFChars(env, prompt, &isCp);
    if (str == NULL) {
        return NULL; /* OutOfMemoryError already thrown */
    }
    if(isCp == JNI_TRUE)
    {
        strcpy(str,"12345");
    }
    printf("%s", str);
    (*env)->ReleaseStringUTFChars(env, prompt, str);
    /* We assume here that the user does not type more than
     * 127 characters */
    scanf("%s", buf);
    return (*env)->NewStringUTF(env, buf);
}
/*
set java_inc=E:\FILES\java\jdk1.6.0_29\include
cl -I%java_inc% -I%java_inc%\win32 -LD Prompt.c -FePrompt.dll
*/
/* output
the output of running class Prompt.class:
12345jdkfjkdjfkdf
User typed: jdkfjkdjfkdf
Type a line:

the output demonstrates that String instance temp did not changed, and isCopy is JNI_TRUE
*/


// example-2: GetIntArrayElements()
public class TestJNI
{
    public native void intArray(int[] ii);
    public static void main(String[] args)
    {
        System.loadLibrary("TestJNI");
        TestJNI jni = new TestJNI();
        int[] ii = new int[4];
        for(int i = 0; i < ii.length; i++)
        {
            ii[i] = i;
        }
        jni.intArray(ii);
        for(int i = 0; i < ii.length; i++)
        {
            System.out.println(ii[i]);
        }
    }
}
/*
output:
isCopy: 1
100
101
102
103

the output shows that ii has been changed, but isCopy is JNI_TRUE
*/

#include "TestJNI.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_TestJNI_intArray(JNIEnv *env, jobject obj, jintArray intArr)
{
    jboolean isCp;
    int i;
    jsize len = (*env)->GetArrayLength(env, intArr);
    jint *arr = (*env)->GetIntArrayElements(env, intArr, &isCp);
    if(isCp == JNI_TRUE)
    {
        printf("isCopy: JNI_TRUE\n");
    }
    for(i = 0; i < len; i++)
    {
        arr[i] = 100 + i;
    }
    (*env)->ReleaseIntArrayElements(env, intArr, arr, 0);
}
/*
output:
isCopy: 1
100
101
102
103

the output shows that ii has been changed, but isCopy is JNI_TRUE
*/

example-1:
the output demonstrates that String instance temp did not changed, and isCopy is JNI_TRUE
example-2:
the output shows that ii has been changed, but isCopy is JNI_TRUE
if isCopy is set to JNI_TRUE, which means arr points a new array, but intArr changed, why?

  • 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-03T12:07:50+00:00Added an answer on June 3, 2026 at 12:07 pm

    ReleaseIntArrayElements() commits any changes you have made. String is immutable, that’s why there is no fourth argument to ReleaseStringUTFChars(). I would say both examples are invalid as they don’t supply the same addresses when releasing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example model: public class Thing { [JsonProperty(foo)] public string Foo {get;set;} [JsonProperty(bars)] public Dictionary<string,string>
Example class code: <?php class example { public function forExample() { return true; }
Example, let's say from a game: public class Enemy { protected int x, y;
Example: public class BusinessTransactionFactory<T> where T : IBusinessTransaction { readonly Func<Type, IBusinessTransaction> _createTransaction; public
Example: public abstract class BaseControler : Controller { public IUnitOfWork UnitOfWork { get; set;
Example: var sessions = require('sessions'); function load_session(req) { sessions.load(sid); } Now that the session
Example (tested in Chrome): <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN> <HTML><HEAD> <TITLE>testing cursor
example: NSString *day = [[NSString stringWithFormat:@السبت];and i think the representation of this string in
Example input string: (F1 (F2 X (Y) Z) (F3 A B) What i want
Example: error_reporting(E_ALL | E_STRICT); class Test {} $obj = new Test(); $obj->undeclared = oops;

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.