Q:

how to modify the value of a jstring passed to a C++ routine using Java and JNI?

Can I pass a string from Java to my C++ routine using JNI function calls and modify its value in the C++ routine?
So far, I have seen examples of returning jstring, which I do not want to do. The other option that I know about is to get the ID of the string variable within C++ and set its value.
At present, I am playing with a function like the following:
JNIEXPORT void JNICALL Java_myexample_ChangeString
(JNIEnv *, jobject obj, jstring strJava) 

And I want to change strJava's value. So, essentially what I am asking is if it Java can pass variables by reference and not just by value.
Thanks.

A:

Java strings are immutable by design, you cannot change them, not even with JNI.

A:

So, it seems that what you really want to do is return more than one value from a native method. There is no direct way to do this, but there are a few indirect ways:

Instead of returning a boolean, create a class that contains both a boolean and a String, and have your native code create and return an instance of that class.
Pass a one-element String[] as an argument, and have the native code set its string result as the first element of that array.
Create a class with a #setStringValue(String) method, and pass that into your native method, which will then call that method to supply the string result. Once the native call completes, the Java code that created the helper class can extract the string value.

Of these possibilities, the one-element array is probably the easiest to handle from JNI code because it doesn't involve looking up additional classes or methods. However, if I wanted to return extra information from a non-native method I would probably return an instance of a new class containing both results.

