<언어>/[Java]
[Java] Why Java Strings are immutable
콜리브리
2022. 4. 3. 23:33
https://www.youtube.com/watch?v=Bj9Mx_Lx3q4
Saves memory by making different variables refer to the same string literal
(1 balloon, two strings; balloon being string literal "John" and strings being the two variables)
String name = "John";
String anotherName = "John";
System.out.println(name == anotherName); // True
// == checks if two operands point to the same memory
// 같은 메모리를 가리키는지 체크
Caution) If a new String instance is explicitly created, another memory is allocated
String name = "John";
String anotherName = "John";
String totallyAnotherName = new String("John");
System.out.println(name == totallyAnotherName); // False
Python also states strings to be immutable - same reason
https://goldenriver42.tistory.com/248?category=936334
(미완) https://goldenriver42.tistory.com/239?category=923013