M
Mark Space
Just cruising around the Java API, I found the Segment class:
<http://java.sun.com/javase/6/docs/api/javax/swing/text/Segment.html>
"A segment of a character array representing a fragment of text. It
should be treated as immutable even though the array is directly
accessible. This gives fast access to fragments of text without the
overhead of copying around characters. This is effectively an
unprotected String.
"The Segment implements the java.text.CharacterIterator interface to
support use with the i18n support without copying text into a string. "
Would having an actual immutable array type make this better? It seems
to me that it comes up rather often (not very often, but often enough)
that mutable arrays are a problem.
For example, making of defensive copy of an internal array in an
immutable type. Just returning an immutable array could be a big
performance gain in many instances.
I'm not sure how this could work. Maybe just a trick of the language
that makes assignment a runtime exception.
char [] c1 = new char[10];
// Fill c1...
char [] c2 = Arrays.immutable( c1 );
Now an attempt to write to c2 would generate a runtime error. The
reflection API will need a new type, maybe derived from
java.lang.reflect.Array, for the immutable type.
Any thoughts? I'm I off my rocker here? A lone voice crying in the
wilderness?
<http://java.sun.com/javase/6/docs/api/javax/swing/text/Segment.html>
"A segment of a character array representing a fragment of text. It
should be treated as immutable even though the array is directly
accessible. This gives fast access to fragments of text without the
overhead of copying around characters. This is effectively an
unprotected String.
"The Segment implements the java.text.CharacterIterator interface to
support use with the i18n support without copying text into a string. "
Would having an actual immutable array type make this better? It seems
to me that it comes up rather often (not very often, but often enough)
that mutable arrays are a problem.
For example, making of defensive copy of an internal array in an
immutable type. Just returning an immutable array could be a big
performance gain in many instances.
I'm not sure how this could work. Maybe just a trick of the language
that makes assignment a runtime exception.
char [] c1 = new char[10];
// Fill c1...
char [] c2 = Arrays.immutable( c1 );
Now an attempt to write to c2 would generate a runtime error. The
reflection API will need a new type, maybe derived from
java.lang.reflect.Array, for the immutable type.
Any thoughts? I'm I off my rocker here? A lone voice crying in the
wilderness?