inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

hope님의 게시글

hope hope

@heemanglee

수강평 작성수
3
평균평점
5.0

게시글 1

질문&답변

문자열 리터럴과 문자열 객체 관련해서 질문이 있습니다.

따라서 intern()의 경우에도 다음 흐름처럼 작동될 것이라 생각을 했습니다. public class Main { public static void main(String[] args) { // 문자열 리터럴 s1는 JVM Heap 영역의 String Constant Pool에 저장된다. // s2는 문자열 리터럴에 존재하는 "hello" 문자열을 참조한다. String s1 = "hello"; String s2 = "hello"; System.out.println(s1.equals(s2)); // true System.out.println(s1 == s2); // true // 문자열 상수 풀에 "hello" 문자열이 존재하므로, // 문자열 상수 풀로부터 문자열 객체를 가져온다. System.out.println(s1.intern()); // hello System.out.println(); // s3와 s4는 JVM Heap 영역에 저장된다. String s3 = new String("hello"); String s4 = new String("hello"); System.out.println(s3.equals(s4)); // true System.out.println(s3 == s4); // false, JVM Heap에 저장된 메모리 주소가 다르다. System.out.println(); System.out.println(s1 == s3); // false, JVM Heap에 저장된 메모리 주소가 다르다. System.out.println(s1.equals(s3)); // true, 문자열 값이 일치한다. } }

좋아요수
0
댓글수
3
조회수
379