1

I would like to know what is the difference between 2 ways of declaring String as the example below:

String str = "Java String";

String str = new String("Java String);
AndroidTa
  • 79
  • 6

1 Answers1

1

Simply put, new String("Java String"); explicitly creates a new and referentially distinct instance of a String object;

String s = "Java String"; may reuse an instance from the string constant pool if one is already available.

So if you have already created a String object with the value of "Java String", no new String will be created here.

Supun Amarasinghe
  • 1,443
  • 3
  • 12
  • 24