Why is it not covered in java docs of String class about how many objects will be created and where?
It is covered in 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.
Strings 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 example:
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 (unchanging) value.
String literals (§3.10.5) are references to instances of class String.
And from JSL # 3.10.5. String Literals
Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
.
Why new String(String) is provided anyway in String class provided Strings are immutable?.Also can it can be assumed that all strings, created by either String s = "abc" or String s = new String("abc"), will be available in String constant pool?
Since String is object the valid way of declaration is
String s = new String("abc");
But where String s = "abc";
is designed for other reasons
The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language.
Since it is the most useful class For performance reason, Java's String is designed to be in between a primitive and a class.
The String literals used in creating or appended in StringBuilder or StringBuffer,do they also go in String constant pool or they remain in heap memory only.
Consider the example
StringBuilder sb = new StringBuilder("abc");
The literal "abc"
available in constant pool and the object sb
created in heap.
Give a shot to read my old answer : How can a string be initialized using " "?