St.Antario
St.Antario

Reputation: 27425

Does NewStringUTF copy the const char * passed in

Official JNI API does specify if the copy is made while creating a jstring from const char *. Here is the quote:

NewStringUTF
jstring NewStringUTF(JNIEnv *env, const char *bytes);

Constructs a new java.lang.String object from an array of characters in modified UTF-8 encoding.

Does it mean the copy of const char * is made or I have to strdup it by myself?

Upvotes: 2

Views: 1008

Answers (1)

apangin
apangin

Reputation: 98495

NewStringUTF creates a new String object in Java Heap. The string is backed by a byte[] or char[] array in Java Heap, and it does not share any off-heap data you pass in.

So, you don't need strdup, the contents will be copied (and possibly converted) to Java Heap anyway.

Upvotes: 4

Related Questions