
String Constant Pool, feat. Runtime Constant Pool
·
💻 Dev/Java & OOP
String str = "hello";String str2 = "hello";System.out.println(str == str2); // true위 코드에서 str == str2의 결과는 뭘까?동등성과 동일성을 배웠다면 false로 예상했겠지만 결과는 true다. "어라 자바에서 객체는 ==이 아니라 .equals()로 비교해야 문자열 비교가 되는 거 아니었나?"그 이유는 JVM이 String Pool을 사용하기 때문이다. String Constant Pool이란?우선 Contant Pool이란 클래스 내에 사용되는 상수들을 담아놓은공간이다.클래스 파일(.class)안에 테이블 형태로 들어가 있다. 이 Constant Pool에서 문자열 리터럴만을 별도로 관리하는 공간이 String Pool(Stri..