Ways to instantiate a Java String

Asked

Viewed 277 times

-2

What is the difference between these two ways of instantiating a Java String?

String x = "y";

String x = new String("y");

1 answer

1

Basic difference is that the first example will be in the string pool and the second in the object memory.

Also use the String x = new String("y"); is slower than the other method.

Check out this link to understand the string pool Performance in creating strings in Java

Browser other questions tagged

You are not signed in. Login or sign up in order to post.