๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

[Java] StringBuffer์™€ StringBuilder

by dkswnkk 2022. 4. 6.

์„œ๋ก 

 

[Java] String ํด๋ž˜์Šค

์„œ๋ก  c์–ธ์–ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์„ charํ˜•์˜ ๋ฐฐ์—ด๋กœ ๋‹ค๋ฃจ์—ˆ์ง€๋งŒ ์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์ž์—ด์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ String ํด๋ž˜์Šค์ธ๋ฐ, String ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ํ•„

dkswnkk.tistory.com

์ด์ „์— String ํด๋ž˜์Šค์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. String ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ง€์ •๋œ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ง์…ˆ ์—ฐ์‚ฐ์ž '+'๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ๊ฒฝ์šฐ ๋งค ์—ฐ์‚ฐ ์‹œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๊ฐ€์ง„ String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ StringBuffer ํด๋ž˜์Šค๋Š” ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” ์ด๋Ÿฌํ•œ StringBuffer๊ฐ€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  StringBuilder๋Š” ๋˜ ๋ฌด์—‡์ธ์ง€ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. StringBuffer

StringBuffer๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฌธ์ž์—ด ํŽธ์ง‘์„ ์œ„ํ•œ ๋ฒ„ํผ(buffer)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, StringBuffer ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ทธ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public final class StringBuffer implements java.io.Serializable{
    private char[] value;
}

์œ„ ์ฝ”๋“œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, StringBuffer ํด๋ž˜์Šค๋Š” String ํด๋ž˜์Šค์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ charํ˜• ๋ฐฐ์—ด์˜ ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด ๋†“๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. StringBuffer ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, charํ˜• ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋˜๋ฉฐ ์ด๋•Œ ์ƒ์„ฑ๋œ charํ˜• ๋ฐฐ์—ด์„ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ value๊ฐ€ ์ฐธ์กฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

1) StringBuffer์˜ ์ƒ์„ฑ์ž

StringBuffer ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ์ ์ ˆํ•œ ๊ธธ์ด์˜ charํ˜• ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋˜๊ณ , ์ด ๋ฐฐ์—ด์€ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ํŽธ์ง‘ํ•˜๊ธฐ ์œ„ํ•œ ๊ณต๊ฐ„(buffer)์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

StringBuffer ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ์ƒ์„ฑ์ž StringBuffer(int length)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒ์„ฑํ•˜๊ณ , ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด 16๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์˜ ๋ฒ„ํผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

public StringBuffer(int length) {
    value = new char[length];
    shared = false;
}

public StringBuffer() {
    this(16);   // ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋Š” 16์ด ๋œ๋‹ค.
}

public StringBuffer(String str) {
    this(str.length() + 16);    // ์ง€์ •ํ•œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ณด๋‹ค 16์ด ๋” ํฌ๊ฒŒ ๋ฒ„ํผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    append(str)
}

๋งŒ์•ฝ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์—…ํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์ž‘์—…์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. 

๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ƒˆ๋กœ์šด ๊ธธ์ด์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ํ›„์— ์ด์ „ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€์˜ ArrayList๋ถ€๋ถ„์—์„œ ์ •๋ฆฌํ•œ ๋ฐ”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

[Java] ArrayList vs LinkedList

์„œ๋ก  List๋Š” ๋ชจ๋“  ์–ธ์–ด์—์„œ ๊ฐ€์žฅ ์œ ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. Java์—์„œ๋Š” List๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•˜๋ฉฐ, LinkedList์™€ ArrayList๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ArrayList์™€ LinkedList์˜ ๊ณตํ†ต๋œ ๋ถ€๋ถ„์„

dkswnkk.tistory.com

 

2) StringBuffer์˜ ๋ณ€๊ฒฝ

String๊ณผ ๋‹ฌ๋ฆฌ StringBuffer๋Š” ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€๋Š” StringBuffer์—์„œ์˜ ๋‚ด์šฉ ๋ณ€๊ฒฝ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

StringBuffer์˜ ๋‚ด์šฉ ๋ณ€๊ฒฝ

๋˜ํ•œ StringBuffer์—์„œ append() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…์ด StringBuffer์ธ๋ฐ ์ž์‹ ์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("abc");    // abc๋ฅผ ๊ฐ€์ง„ sb๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (ํฌ๊ธฐ๋Š” 2+16)
    StringBuffer sb2 = sb.append("ZZ");    // sb์˜ ๋‚ด์šฉ ๋’ค์— "ZZ"๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ sb์˜ ์ฃผ์†Œ๋ฅผ sb2๊ฐ€ ๊ฐ€๋ฅดํ‚จ๋‹ค.
    System.out.println(sb);    // abcZZ
    System.out.println(sb2); // abcZZ
}

๊ทธ๋ž˜์„œ ์œ„ ์ฝ”๋“œ์—์„œ sb.append("ZZ")๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„ sb์˜ ์ฃผ์†Œ๋ฅผ sb2๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์— sb์™€ sb2๋Š” ์„œ๋กœ ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋˜์–ด ์ถœ๋ ฅ ์‹œ ๋‘˜ ๋‹ค "abcZZ" ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

3) StringBuffer์˜ ๋น„๊ต

String ํด๋ž˜์Šค์—์„œ๋Š” equals ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ๋น„๊ตํ•˜๋„๋ก ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€๋งŒ, StringBuffer ํด๋ž˜์Šค๋Š” equals ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์ง€ ์•Š์•„์„œ StringBufferํด๋ž˜์Šค์˜ equals ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ "==" ์—ฐ์‚ฐ์œผ๋กœ ๋น„๊ตํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("abc");
    StringBuffer sb2 = new StringBuffer("abc");

    System.out.println(sb == sb2);      // false
    System.out.println(sb.equals(sb2)); // false
}

๋ฐ˜๋ฉด์— toString()์€ ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋˜์–ด ์žˆ์–ด์„œ StringBuffer ์ธ์Šคํ„ด์Šค์— toString()์„ ํ˜ธ์ถœํ•˜๋ฉด, ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์„ String์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ StringBuffer ์ธ์Šคํ„ด์Šค์— ๋‹ด๊ธด ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด StringBuffer ์ธ์Šคํ„ด์Šค์— toString()์„ ํ˜ธ์ถœํ•ด์„œ String ์ธ์Šคํ„ด์Šค๋ฅผ ์–ป์€ ๋‹ค์Œ, ์—ฌ๊ธฐ์— equals ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋น„๊ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("abc");
    StringBuffer sb2 = new StringBuffer("abc");

    String s = sb.toString();
    String s2 = sb2.toString();

    System.out.println(s == s2);        // false
    System.out.println(s.equals(s2));   // true
}

 

2. StrungBuilder

์‚ฌ์‹ค StringBuilder์™€ StringBuffer์˜ ์ฐจ์ด๋Š” ์Šค๋ ˆ๋“œ์˜ ๋™๊ธฐํ™” ๊ธฐ๋Šฅ์ด ์žˆ๋ƒ ์—†๋ƒ์˜ ์ฐจ์ด๋งŒ ์žˆ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์™„์ „ํžˆ ๋˜‘๊ฐ™์€ ๊ธฐ๋Šฅ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • StringBuffer๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ์— ์•ˆ์ „(thread safe)ํ•˜๋„๋ก ๋™๊ธฐํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • StringBuilder๋Š” StringBuffer์—์„œ ์Šค๋ ˆ๋“œ์˜ ๋™๊ธฐํ™”๋งŒ ๋บ€ ์ฑ„๋กœ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ 

 

Java String, StringBuffer, StringBuilder

1- Hierarchical Inheritance When working with text data, Java provides you with three classes including String, StringBuffer and StringBuilder. When working with big data, you should use StringBuffer or StringBuilder to optimize the efficiency. Basic

o7planning.org

 

๋Œ“๊ธ€