I am reading the source code of String.java
in OpenJDK jdk16u.
One of its constructors is shown as follows:
/**
* 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;
this.coder = "".coder;
}
Apparently that .value
refers to this private byte
array:
/**
* The value is used for character storage.
*
* @implNote This field is trusted by the VM, and is a subject to
* constant folding if String instance is constant. Overwriting this
* field after construction will cause problems.
*
* Additionally, it is marked with {@link Stable} to trust the contents
* of the array. No other facility in JDK provides this functionality (yet).
* {@link Stable} is safe here, because value is never null.
*/
@Stable
private final byte[] value;
This means that when we use String() as a constructor, this.value references "".value.
However, I can't find the exact definition of "".value
. It seems to me like a circular definition.
Edit: it's not circular definition. what I mean is that how does Java know that "" means empty string.