```
웹서버가 String으로 도배돼있다고요?
그럼 한번 이 글을 읽어보세요
String
StringBuffer
StringBuilder
에 대한 요약 설명
```

이번 글을 통해 배워갈 내용
- 많은 웹 개발자들이 StringBuffer를 쓰는 이유
- 간략한 JAVA StringBuilder, StringBuffer, String 설명
String
String Class는 문자열을 나타냅니다.
"example"과 같은 자바 프로그램의 모든 String Literal은 String Class의 인스턴스로 구현됩니다.
문자열은 Constant입니다. 따라서 변경할 수 없는 (Immutable) 속성을 가집니다.
StringBuilder
변경 가능한 문자열 시퀀스 (the mutable sequence of characters)
동기화를 보장하지 않음 (provides no guarantee of synchronization)
StringBuffer보다 빠름
StringBuffer
변경 가능한 문자열 시퀀스 (the mutable sequence of characters)
동기화를 보장 (provides a guarantee of synchronization)
StringBuilder보다 느림
StringBuilder (예시)
public class Test {
public static void main(String[] args) {
// StringBuilder Object 생성
// 문자가 없고 초기 용량이 16자인 StringBuilder 생성자 호출
StringBuilder sb = new StringBuilder();
// StringBuilder 의 크기를 늘리고 문자를 추가
sb.append("Hello");
// StringBuilder 출력
System.out.println(sb);
}
}
StringBuffer (예시)
public class Test {
public static void main(String[] args) {
// StringBuffer Object 생성
// 문자가 없고 초기 용량이 16자인 StringBuffer 생성자 호출
StringBuffer sb = new StringBuffer();
// StringBuffer 의 크기를 늘리고 문자를 추가
sb.append("Hello");
// StringBuffer 출력
System.out.println(sb);
}
}
3줄 요약
Thread safe 하고 문자열을 변경한다면 String Buffer를 쓴다
Thread safe 하지 않다는 게 확실하고 문자열을 변경한다면 String Builder를 쓴다
매우 짧은 String을 간단하게 쓰는 경우 String을 쓴다
참조
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String (Java Platform SE 7 )
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum
docs.oracle.com
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
StringBuffer (Java Platform SE 7 )
Inserts the string into this character sequence. The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by
docs.oracle.com
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
StringBuilder (Java Platform SE 7 )
Inserts the string into this character sequence. The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by
docs.oracle.com
https://www.geeksforgeeks.org/string-vs-stringbuilder-vs-stringbuffer-in-java/
String vs StringBuilder vs StringBuffer in Java - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder
Difference between StringBuffer and StringBuilder - javatpoint
Difference between StringBuffer and StringBuilder in java, let's see the StringBuffer vs StringBuilder in java with examples, there is given a list of main differences between StringBuffer and StringBuilder.
www.javatpoint.com
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
'Java > Java 기타' 카테고리의 다른 글
| For loop 대신 Stream을 사용하는 2가지 이유 (0) | 2022.05.15 |
|---|---|
| JAVA 공백으로 띄운 스트링 입력 정수 리스트로 변환하는 3가지 방법 (1) | 2022.05.07 |
| JAVA로 시스템 환경 값 가지고 오기 (0) | 2022.03.25 |
| JAVA로 시스템 속성에 값넣고 값가지고 오기 (0) | 2022.03.25 |
| 자바에 리스트 내부 중복된 원소 지우는 두가지 방법 (0) | 2022.02.24 |