Ways to instantiate a String in Java [duplicate]

-2

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

String x = "y";

String x = new String("y");
    
asked by anonymous 21.11.2018 / 19:38

1 answer

1

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

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

Check out this link to understand the string pool String performance in Java

    
21.11.2018 / 19:40