The simple answer is zero objects. That is a compile time constant expression, and the bytecode compiler evaluates it to "abcd" ... before creating the ".class" file.
Actually, with modern JVMs, the instantiation of String objects associated with literals and compile time constant expressions is lazy, so a single String object may be created the first time that statement is executed. But only the first time.
So a more correct answer is either zero or one String objects, depending on:
- the JVM implementation of string literal interning (lazy or eager), and
- whether this is the first execution of any statement that uses the
"abcd" literal or compile time constant.
Then there is the possibility that the statement might be optimized away by the JIT compiler if str is never accessed.
And it gets even more complicated if you consider the possibility of class unloading.
String. There is noStringBuilderin your line of code...literalstring , object will not be created. because all will be string pooled.