4

So I was reading String class when i stumbled on one confusing constructor. The code goes like this

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {

    /** The value is used for character storage. */
    private final char value[];

    /** Initializes a newly created {@code String} object so that it represents
    * an empty character sequence.  Note that use of this constructor is
    * unnecessary since Strings are immutable.
    */
    public String() {
        this.value = "".value;
    }
// the rest of the class code
}

I don't understand what does

"".value;

do. What is this ""? Is it a new String object? If it is, with what constructor?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Daniel
  • 980
  • 9
  • 20
  • 3
    this means string with 0 length – bananas May 14 '16 at 11:16
  • this is simple String literal object resides on String pool.. see this http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext – rev_dihazum May 14 '16 at 11:32
  • Can you provide a link to the code in question? Because OpenJDK [7](http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/lang/String.java#l151) and [8](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l138) have `this.value = new char[0];`. – dhke May 14 '16 at 11:33
  • @dhke I unpacked src.zip from jdk1.8.0_71 [https://gist.github.com/anonymous/1ab847ab5a108637cd7bbcf82026a6cf#file-string-java-L138](gist line 138) – Daniel May 14 '16 at 11:43
  • @ThrashAbaddon It's also in JDK9. See my answer, below and also for possible rationale. – dhke May 14 '16 at 11:55

2 Answers2

1

What is this ""? Is it a new String object?

It's an empty string. It does not have to be new and most likely isn't, since string literals are interned.

If it is, with what constructor?

At compile time the empty string was probably created by the source code Scanner from a sequence of input characters using String(char[], int, int).

At runtime, the ClassLoader has loaded and interned the String from the class file in native code probably via java_lang_String::create_from_unicode() and friends.

As for why using "": It is a memory use optimization. Since string literals are interned, "".value is a reference to the same underlying char array for every empty string.

From the repositories, OpenJDK (7, 8) previously used this.value = new char[0];. A new char array object was created for every new String() appearing in source code (i.e. hopefully never).

Current OpenJDK 9 has "".value.

The change prevents creating a new char array and thus saves a few bytes per non-interned, but newly created empty string. After the change, all zero-length strings share the same (zero-length) char array.

I'd consider this a rare case, because I cannot recall if I ever used new String() anywhere.

dhke
  • 15,008
  • 2
  • 39
  • 56
  • I really shouldn't write comments at 3 a.m., *mea culpa,* but there still isn't any such thing as '*the*' Java compiler. Describing what one implementation does can't possibly answer the question properly. – user207421 May 17 '16 at 01:13
  • @EJP You are right there, of course, that where the *probably* comes in, which should be read as a short form of *If using the standard JDK compiler, then ...*. But the answer can also do without the breadcrumb trail of where the string comes from in the first place. – dhke May 17 '16 at 06:01
0

"" is a String and thus has a char[] values as any String would. However, it has no characters, making values an empty array.

Basically, the code initializes values to an empty array.

No Name
  • 131
  • 1
  • 10