Code:
jclass strCls = env->FindClass("java/lang/String");
jmethodID mid = env->GetMethodID(strCls,"<init>","([C)V");
jstring strUnicode = env->NewStringUTF("aaa");
jint len = env->GetStringUTFLength(strUnicode);
LOGD("GetStringUTFLength:%d",len);
jint len1 = env->GetStringLength(strUnicode);
LOGD("GetStringLength:%d",len1);
The above code prints: len and len1 are both 3. So what is the difference between GetStringLength and GetStringUTFLength?
Take a look at the description of the jni document:
which is:
(1) jsize GetStringLength(jstring string): returns the number of characters in the Unicode string.
(2) jsize GetStringUTFLength(jstring string): returns the number of bytes of the UTF-8 string, not including the end'\0'.
for example:
"aaa"
jchar*(2 bytes)~Unicode string
char*(1 byte)~UTF-8 string
(1) GetStringLength() returns the number of characters in the Unicode string "aaa",
That is to treat "aaa" as a Unicode string, so there are 3 characters in total, each of which is of type jchar, and a total of 6 bytes.
(2) GetStringUTFLength() returns the number of bytes of the UTF-8 string "aaa",
It is to treat "aaa" as a UTF-8 string, so there are 3 characters in total, each of which is of char type, with a total of 3 bytes.