As with all other Java object types, java.lang.String is mapped to a proxy class, java_lang_String. This proxy class has a few special features:
A C++ constructor that takes a char* parameter (assumes UTF-8 encoding):
String s1("abc");
An overloaded assignment operator, which allows you to assign a string in any of the following ways:
String s2 = "xyz"; String s3 = String("xyz"); String s4 = new_String("xyz");
A str method that returns a char* (UTF-8 format). The return value is automatically freed when the String from which it was obtained goes out of scope. The following code segment prints “xyz” to standard output:
String s2 = "xyz"; char* s2Str = s2.str(); cout << s2.str();
A strcpy method that returns a char* (UTF-8 format). The return value is a copy, which is not automatically freed. You must free it explicitly using the delete[] operator. The following code segment prints “xyz” to standard output, then frees the memory associated with s2Str.
String s2 = "xyz"; char* s2Str = s2.strcpy(); cout << s2Str; delete[] s2Str;
To pass a String constant in a parameter of type Object, you can use an explicit String constructor, as illustrated in the following code segment:
java_util_HashMap map = new_java_util_HashMap(); map.put(String("abc"), String("xyz"));