am i doing anything wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

am i doing anything wrong?

string a="somnath"; String b="somnath"; if String was mutable..if i make any change in somnath..both will show the same result as both a and b were referring somnath.. if i m right..why it is not happening in case of StringBuilder.. class StringBuilderMutable { public static void main(String args[]) { StringBuilder r=new StringBuilder("somnath"); StringBuilder r2=new StringBuilder("somnath"); r.replace(1,2,"s"); System.out.print(r+" "+r2); } } why it is not showing same result ?

3rd Jun 2017, 6:43 AM
Somnath Ghosh
Somnath Ghosh - avatar
3 Answers
+ 2
class StringBuilderMutable { public static void main(String args[]) { String a="somnath"; String b="somnath"; a = a.substring(0,1)+'s'+a.substring(2); System.out.print(a+" "+b); } } I'm not sure what you were trying to do, but this code does the same result as the lower code you had. Hope it helps!
3rd Jun 2017, 7:16 AM
Miska Rantala
Miska Rantala - avatar
0
thank u..but what am i doing wrong ?,,i want to get the same output
3rd Jun 2017, 8:48 AM
Somnath Ghosh
Somnath Ghosh - avatar
0
r and r2 not point the same reference.
3rd Jun 2017, 10:26 AM
Szabó Gábor
Szabó Gábor - avatar