Search code examples
javac++stringoptimizationjava-native-interface

GetStringUTFChars and its string copy behavior


In the JNI document, GetStringUTFChars(), which converts a java string jstring to c++ const char*, will return an optional jboolean flag indicating whether it performs copy or not within the function call. However, the document does not mention when GetStringUTFChars() will or will not perform copy. My questions are:

  1. Under which condition GetStringUTFChars() will perform a copy?
  2. Is there any way to avoid copy in GetStringUTFChars()?
  3. If the answer to question 2. is true, is it suggested to avoid such copy?

Solution

  • According to the book "Essential JNI Java Native Interface", it is the implementation of the JVM that decides whether a copy is done or not. So no, you have no control over the copying.