Consider statement:
String s=new String(“abc”);
Will this statement creates two String objects namely “abc” and the one represented by ‘s’?
and if it creates two objects then will “abc” get stored in String pool or just discarded?
EDIT:
i am asking this question in reference to Difference between string object and string literal, where in the last two answers , creation of two objects is denied.
Avoid such kind of behavior , because
"abc"is already aStringand by making anew String, you are creating an unnecessaryObject.Instead go for
String s = "abc";This way, the
Stringgets interned by the JVM and is added to a pool.To answer your question, you are just creating an
Object sthat is referring to"abc".So when you do say
String t = new String("abc");and then dos==t, will yield infalse. Because they have their separate instances toabc.