No.
You can test it by running the following code.
String s = new String("Hello");
String upperS = "HELLO";
String s1 = s.toUpperCase();
System.out.println(s==s1); //prints false
System.out.println(upperS==s1); //prints false
From §3.10.5 of the Java documentation:
…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.29) - are "interned" so as to share unique instances, as if by execution of the method String.intern (§12.5).
So, the String Pool holds only string literals from the Java source code (expressions like "abc"
) and strings that have had their .intern
method called.
In your code the following line creates a String object on the heap, but not a literal in the String Pool.
String s = new String("Hello");
The following line also creates another object in the Heap and doesn't create a literal in the String Pool.
String s1 = s.toUpperCase();
So, in your example. 2 objects will be created in the heap. No new literals will be added to the String Pool.
Take a look at this answer