Skip to content

Commit cf3a418

Browse files
committed
Show that toCharArray() and new String(char[]) both copy
The example now prints the original String after changing the array, and changes the array again after creating the new String, so both copies are visible.
1 parent b6c8c91 commit cf3a418

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎src/main/java/eu/happycoders/string/basics/Ch10Converting.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ static void main() {
1818
System.out.println(String.valueOf(true) + " " + String.valueOf('x'));
1919

2020
System.out.println("== char[]");
21-
char[] chars = "Java".toCharArray();
21+
String java = "Java";
22+
char[] chars = java.toCharArray();
2223
chars[0] = 'L';
23-
System.out.println(new String(chars));
24+
System.out.println(java);
25+
26+
String lava = new String(chars);
27+
chars[0] = 'J';
28+
System.out.println(lava);
2429

2530
System.out.println("== byte[]");
2631
byte[] utf8 = "Grüße".getBytes(StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)