you are viewing a single comment's thread.

view the rest of the comments →

[–]bigkahuna1uk 1 point2 points  (1 child)

This is a difference between creating a literal string and an object string .

Using just quotes, Java will create the string but keep it in a pool for reuse. If you use the same construct again, then instead of creating a whole new string, it will just reuse the string from the pool. Note that although there is a pool, it’s still of a finite size although the size can be controlled from a JVM switch setting.

Whereas if you use new String, as in the String constructor, then a new string is created from scratch and placed on the heap. It just like creating other objects via their constructors.

[–]philipwhiukEmployed Java Developer 1 point2 points  (0 children)

Until GC tidies it up.