1

As Both expression gives String object, but there is difference between them. When we create String object using new() operator, it always create a new object in heap memory. On the other hand, if we create object using String literal syntax e.g. String s2 = "Test"; it may return an existing object from String pool , if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

What if we create a "Test" String in heap memory using new key word.Does String literal point to already created String(Which i created using new keyword) or not?

Rauf javid
  • 77
  • 8
  • `it may return an existing object from String pool , if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.`---this is not correct. The evaluation of a string literal expression never causes object creation. All literals are guaranteed to already exist in the pool. – Marko Topolnik Mar 23 '15 at 19:20

0 Answers0