.NET on a string

Robin KAY komadori at gekkou.co.uk
Mon Mar 23 13:04:01 PDT 2009


Steven Schveighoffer wrote:
[snip]
> I thought one of the benefits of having immutable strings is that 
> substrings were just pointers to slices of the original data in Java and 
> .NET.  So every time I do a substring in Java and .NET, it creates a 
> copy of the data?  That seems very wasteful, especially when the data is 
> immutable...

Sun's Java implements the String class as below, so that sub-strings can
share their parent's character array. This is, AFAIK, an implementation
detail although I don't know of any implementation that differs.

The downside is that holding a reference to a sub-string will prevent
the parent from being collected and this is not an obvious side-effect
of substring(). Storing longer strings as ropes might be better.

class String
{
   char[] value;
   int offset;
   int count;
}

-- 
Robin KAY



More information about the Digitalmars-d mailing list