Constructors
The constructors of the class String are useful if you are concerned that the strings your application creates consume too much memory. The problem is that String literals (abc, for example) are stored in a special area of the memory called the "string constant pool" and never garbage collected. The idea behind such a design is that String literals consume substantially more memory than numbers. Also, the handling of such large entities has an overhead that may tax the JVM. That is why the designers figured it is cheaper to store them and share them between all application threads than allocate new memory and then clean it up several times for the same value.
But if the rate of reuse of the String values is low, while the stored ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access