File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -47,13 +47,17 @@ private static void stringBuilderDemo() {
4747 }
4848 String result = sb2 .toString ();
4949 System .out .println (result );
50+
51+ sb2 .setCharAt (2 , 'l' );
52+ sb2 .setCharAt (7 , 'j' );
53+ System .out .println (sb2 );
5054 }
5155
5256 // StringBuilder is essentially a wrapper of char[],
5357 // with some additional funtionalities including resizing the char array.
5458 // If char array can do the job then we should just use char array
5559 private static void modifyString (String str ) {
56- char [] chars = str .toCharArray ();
60+ char [] chars = str .toCharArray (); //Returns a deep copy of the char array in the string
5761 for (char c : chars ) {
5862 System .out .print (c );
5963 }
@@ -63,7 +67,7 @@ private static void modifyString(String str) {
6367 chars [i ] = '#' ;
6468 }
6569 }
66- String modified = String .valueOf (chars );
70+ String modified = String .valueOf (chars ); //Create a new string with a deep copy of chars array
6771 System .out .println ("Modified: " + modified );
6872 }
6973
You can’t perform that action at this time.
0 commit comments