Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- codepresso
- 리액트
- 전문가를위한파이썬
- pyladies
- 파이썬
- 원티드
- mongodb
- AWS
- 개발스터디
- 코테
- 한빛미디어
- pyladiesseoul
- 깃
- 프리온보딩
- Python
- 스터디
- fluentpython
- 코드프레소
- 위코드
- 예리님
- 패스트캠퍼스
- 한빛
- 플라스크
- flask
- cleancode
- React
- env
- 개발
- 환경변수
- git
Archives
- Today
- Total
개발자가 내팔자
[Java] 문자열의 비교 본문
문자열 비교에는 ==
대신 equals()
를 사용해야 한다.
literal string으로 생성하면 string constant pool에 담기고,
new String()으로 생성하면 Heap영역에 생성된다.
원래 string pool은 Perm이라고 하는 영역에 있었다고 한다.
그러나 이는 고정사이즈이기 때문에 runtime에 확장되지 않으므로
string 값이 켜지면 OutOfMemoryException을 발생시킬 수 있어,
자바7부터는 String pool의 위치가 힙영역에 옮겨갔다고 한다.
String Pool이 Flyweight Pattern이라는 디자인 패턴을 구현한 대표적인 예시라고 한다.
예시를 통해 자세히 살펴보자.
Literal string
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2); // true
System.out.println(str1.equals(str2)); // true
literal로 생성한 string이 같은 string이라면 같은 주소를 가지게 된다.
new operator
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1 == str2); // false
System.out.println(str1.equals(str2)); // true
new로 생성할 때마다 새로운 주소에 할당하게 된다.
Reference
https://www.baeldung.com/java-string-pool
Guide to Java String Pool | Baeldung
Learn how the JVM optimizes the amount of memory allocated to String storage in the Java String Pool.
www.baeldung.com
'Programming Language > Java' 카테고리의 다른 글
[Java] Arrays에서 자주 쓰는 메서드 (0) | 2022.07.15 |
---|---|
[Java] String 클래스 (0) | 2022.07.14 |
[Java] 반올림 자릿수 정하기 (0) | 2022.07.14 |
[Java] 연산자 우선순위 (0) | 2022.07.14 |
[Java] 타입 간의 변환 방법 (0) | 2022.07.14 |
Comments