But what happens if the pool already has the literal "abc"? Will there be duplicate literals in the pool?
Not.
String c = new String("abc");
semantically equivalent
String tmp = "abc"; String c = new String(tmp);
Thus, it should be clear that no additional entry is created in the string pool just because the literal is used as an argument to the string constructor.
You are correct that new String(...) creates a string on the heap, so looking at the heap and the string pool, there will be several objects representing "abc" , but not more than one in the string pool.
[...] That is, when we use string literals. But will a literal be used in the string pool, if we give String a = new String("abc"); ?
Yes, the string pool literal will be reused if you give "abc" as an argument to the constructor. The resulting String object, however, will not be in the pool, but in the heap.
source share