[DISCUSSION] Views of immutable objects.

Paulo Levi i30817 at gmail.com
Sat Jul 14 17:52:22 UTC 2007

I'd like to ask if it would be possible to modify the classes of some
primitive immutable types for returning immutable read only views.
For example for strings, when using the document hierarchy, any insertString
in the GapContent object invokes str.toCharArray(), that
is implemented like this:
public char[] toCharArray() {
    char result[] = new char[count];
    getChars(0, count, result, 0);
    return result;
I'd like if the new didn't exist.

Any attempt to use instead the getChars function + class char[] is even
worse, since the local char [] becomes a memory leak.
I think that if the char [] intrinsic classes had a immutable view where
modifying the object was a nop it would eliminate this allocation and keep
string immutable. Something like:
public char[] toImmutableCharArray() {
    ichar result[] = new ichar[count];
    getChars(0, count, result, 0);
    return result;
Preventively I'm going to say I'm not only worried about memory (that might
or might not be later "fixed" by escape analisys stack allocation) but
also about efficiency.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20070714/0de92de0/attachment.html>

More information about the core-libs-dev mailing list