How many objects are created when below statements are executed?
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
Given above java question - I think answer should be 3. First object is created for line# 1, assuming String "abc" didn't existed before. For 2nd line no extra object is created as String literals are interned. For 3rd and 4th statments 2 more objects are created. Hence in total 3 objects are created. This logic is inline with java specs String literal
A string literal is a reference to an instance of class String (§4.3.1, §4.3.3). 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.
What is confusing is java tutorial about String in String
Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.
The use of "whenever" is confusing here. So the real question what can we do to ask people to edit the tutorial to correct it. Otherwise it will confuse lot of people, after all this site is referenced by even experienced professionals.