I assume that "" is not the same as a null string? What is it like in
memory?
It's probably best to think of it as being like an array of length zero.
It's there, but it has nothing in it.
The actual layout in memory is more complex, because String objects are
more complex than arrays: a String is actually a character array, plus two
integers, one being the offset of the start of the string in that array,
and one being the count of characters in the string. For example, the
string "hello" could be represented by a string like this:
characters = {'h', 'e', 'l', 'l', 'o'}
offset = 0 // ^ start
count = 5 // ^ ^ ^ ^ ^ included
But it could equally be:
characters = {'w', 'h', 'y', ' ', 'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e'}
offset = 4 // ^ start
count = 5 // ^ ^ ^ ^ ^ included
The reason for this is that it means that several string objects can share
the same underlying character array which can save memory. This happens
when you start with one big string and then extract substrings from it
(and further substrings from the substrings). An example of when this
might happen is parsing - if you read a whole file into one big string,
then broke out individual lines as strings, then broke each line down into
words or whatever, you might have hundreds of string objects, but you
would only need one copy of the character array for all of them. There are
downsides to this design, but the designers of java evidently felt that
the benefits outweighed the disadvantages.
Anyway, the upshot is that the empty string can have all sorts of possible
layouts in memory, depending on how it was created! The simplest possible
layout is this:
characters = {} // no characters
offset = 0
count = 0
But it could also be:
characters = {'w', 'h', 'y', ' ', 'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e'}
offset = 4 // ^ start
count = 0 // no characters included
The one common factor is that count will always be zero.
tom