What is the difference between String str = new String("SOME") and String str="SOME"
Does these declarations gives performance variation.
What is the difference between String str = new String("SOME") and String str="SOME"
Does these declarations gives performance variation.
String s1 = "Welcome"; // Does not create a new instance
String s2 = new String("Welcome"); // Creates two objects and one reference variable
First one will create new String object in heap and str will refer it. In addition literal will also be placed in String pool. It means 2 objects will be created and 1 reference variable.
Second option will create String literal in pool only and str will refer it. So only 1 Object will be created and 1 reference. This option will use the instance from String pool always rather than creating new one each time it is executed.