data structure & algorithm - (2) : String && StringBuilder && StringBuffer

­이승환·2021년 12월 9일
0

결론


그다지 어렵지 않은 내용이므로 간단하게 정리하겠다.

String

  • immutable 한 속성을 가지고 있음
String str = "hello";
str += " world";
System.out.pringln(str)
// hello wolrd

위 예제에서 우리가 원하는데로 결과가 나오긴 했지만 데이터가 저장되는 heap에 저장소 위치가 변한다(immutable). 따라서 기존의 hello 가 저장되는 메모리 주소는 gc의 타깃이 된다. 따라서 메모리를 재 할당하는 비용이 추가되기 때문에 추가, 삭제, 삽입이 빈번한 경우에는 사용 하지 말것

StringBuffer

  • mutable
  • thread safe

StringBuilder

  • mutable
  • not thread safe
profile
Mechanical & Computer Science

0개의 댓글