First my congrats to Gagan, also would request him to be active on this forum so as it be will be helpful for guys like me and others.
String s1 = "abc";
one object is created at compile time, resides in pool
String s2 = new String("xyz");
two objects are created at compile time, resides in pool and nonpool
s2 = s1;
as the reference is changes , the nonpool reference is eligible for gc
s1.toUpperCase();
one object is created at runtime, is in pool
String s3 = "abc";
already object is in pool
String s4 = s3.replace('a','A');
one object is created at runtime, is in pool
so my answer is 5 is created and one is gc, so finally 4.
there is a lot of discussion on the same subject, i have provided a link already discussed.
https://coderanch.com/t/252556/java-programmer-SCJP/certification/string-function really it is still unclear, may be the person(
Ernest Friedman-Hill) involved in the previous discussion can provide more details, and as requested
K&B also ??
are u guys hearing, it would be of good help if someone of u answers
thanks in advance