💡 문자열 연산이 적고, 조회가 많은 멀티쓰래드 환경에서 좋다.
💡 Thread-safe : 멀티쓰래드 프로그래밍에서 어떤 변수에 동시 접근이 이루어져도 프로그램 실행에 전혀 문제가 없다는 것을 의미한다.
class Main{
public static void main(String[] args) throws Exception{
StringBuffer sb = new StringBuffer("test");
System.out.println(sb + " " + System.identityHashCode(sb));
sb.append("2");
sb.append("3");
System.out.println(sb + " " + System.identityHashCode(sb));
}
}
class Main{
public static void main(String[] args) throws Exception
{
StringBuilder sb = new StringBuilder("test");
System.out.println(sb + " " + System.identityHashCode(sb));
sb.append("2");
sb.append("3"); //문자열 추가
System.out.println(sb + " " + System.identityHashCode(sb));//문자열과 메모리 주소 출력
}
}
💡 StringBuffer : 문자열 연산이 많은 Muti-Thread 환경
💡 StringBuilder : 문자열 연산이 많은 Single-Thread 또는 Thread 신경 안쓰는 환경