0

As I understand that
1) String s="abc"; creates "abc" in string pool and
2) String s=new String("abc").intern(); string pool reference is returned irrespective of whether "abc" exists in pool before or not.

My question is

What is the advantage of 1) over 2) (or vice versa) since both of these returns the reference from pool. Which syntax is preferred?

In 2) irrespective of "abc" present in pool, will the object gets created in heap initially and then its lost? (eligible for gc)

azurefrog
  • 10,785
  • 7
  • 42
  • 56

1 Answers1

3

This is simplest and fastest

 String s = "abc";

Using new String("abc").intern() is not only slower but much more complex and confusing.

In short, don't make the code any more complicated than needed.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130