Discussion Locked

Java: My long strings get corrupted

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Whenever I concatenate a string to an existing very long string, all accentuated characters are replaced by two odd characters. Does anyone know how to resolve this problem?

5 replies

by prakashanthony

Could you please pass on the string values and their declarations?
Try using StringBuffer instead of String and use the StringBuffer.append() to concatenate strings.

by michael

Yes, as noted above, you should use a StringBuffer for any "stringy" object whose value can change.

The Java language doesn't create string "variable" as such - it creates individual String objects as needed to hold literal values and returns handle references to them.

Pointers to existing strings with the desired values are assigned if possible at compile time.

But if you assign a series of values to a String object at run time, you're actually generating a potentially great number of separate objects.

So, use StringBuffer! See the following URL in the Java Language Specification on Sun's Web site:

http://java.sun.com/docs/books/jls/first_edition/html/javalang.doc12.html#14461

This discussion is closed to new replies.