I\'ve been studying Java String for a while. The following questions are based on the below posts
Java String is special
Immutability of String in java
The Java libraries are heavily optimized around the constraint that any String
object is immutable, regardless of how that object is constructed. Even if you create your b
using new
, other code that you pass that instance to will treat the value as immutable. This is an example of the Value Object pattern, and all of the advantages (thread-safety, no need to make private copies) apply.
The empty string ""
is a legitimate String
object just like anything else, it just happens to have no internal contents, and since all compile-time constant strings are interned, I'll virtually guarantee that some runtime library has already caused it to be added to the pool.