[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
'<언어> > [Java]' 카테고리의 다른 글
[Java] Curiously Recurring Generic Pattern (CRTP) (0) | 2024.02.13 |
---|---|
[Java 8] Lambda (0) | 2022.02.24 |
[Java] OOP 공부 (0) | 2022.02.07 |