Why is this not covered in the java docs of the String class about how many objects will be created and where?
This is described in the Docs of String.
The String class represents character strings. All string literals in Java programs, such as "abc," are implemented as instances of this class. Lines are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable, they can be shared. For instance:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
And from the Java language specification
A String object has a constant (immutable) value.
String literals (§3.10.5) are references to instances of the String class.
And from JSL # 3.10.5. String literals
In addition, a string literal always refers to the same instance of the String class. This is because string literals, or, more generally, strings that are constant expression values (§15.28), are “interned” to exchange unique instances using the String.intern method.
.
Why is a new String (String) provided anyway in the String class if the strings are immutable ?. You can also assume that all strings created by either string s = "abc" or String s = new String ("abc") will be available in the String constant pool?
Since String is an object, a valid way of declaring is
String s = new String("abc");
But where is String s = "abc"; designed for other reasons
Java designers decided to keep the primitive types in an object-oriented language, instead of making everything an object in order to improve the performance of the language.
Since this is the most useful class. To ensure performance, Java String is designed to be between the primitive and the class.
String literals used when creating or adding to a StringBuilder or StringBuffer also go to the String constant pool or remain in the heap memory.
Let's look at an example.
StringBuilder sb = new StringBuilder("abc");
The literal "abc" , available in the constant pool, and the sb object created on the heap.
Take a picture to read my old answer: How to initialize a string with ""?