Co-Authored By:

Asked by: Mikolaj Gottgen
technology and computing programming languagesHow many objects are created String s new string?
Moreover, how many string objects are created in the string pool?
If there is no string literal “Cat”in the pool, then it will be first created in thepool and then in the heap space, so a total of 2 stringobjects will be created.
Also to know, what is difference between string and new string?
String strLiteral = "Java"; Both expression givesyou String object, but there is subtle differencebetween them. When you create String object usingnew() operator, it always create a new object in heapmemory. Otherwise it will create a new string object and putin string pool for future re-use.
From Java 7 onwards, the Java String Pool isstored in the Heap space, which is garbage collected by theJVM.