Skip to content

Latest commit

ย 

History

History
106 lines (77 loc) ยท 4.96 KB

String.md

File metadata and controls

106 lines (77 loc) ยท 4.96 KB

String๊ณผ StringBuffer, StringBuilder

written by sohyeon, hyemin ๐Ÿ’ก


1. String ํด๋ž˜์Šค

์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์œ„ํ•œ ํด๋ž˜์Šค์ด๋‹ค. ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ(immutable)ํ•œ ์†์„ฑ์„ ๊ฐ€์กŒ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ String์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์€ ์ฝ์–ด ์˜ฌ ์ˆ˜๋งŒ ์žˆ๊ณ , ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค.

ex) ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ ์˜ˆ์‹œ

String a = "a";
String b = "b";
String a = a+b;

์œ„์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ๋‚ด์˜ ๋ฌธ์ž์—ด์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ
์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ("ab")๊ฐ€ ๋‹ด๊ธด String์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
๋”ฐ๋ผ์„œ ๋ฌธ์ž๋ฅผ ๊ฒฐํ•ฉํ•  ๋•Œ ์—ฐ์‚ฐ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๊ฐ€๋Šฅํ•œ ์—ฐ์‚ฐํšŸ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ด์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์ž‘์—…์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ StringBufferํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

2. StringBuffer

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

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

ex)StringBuffer ์ƒ์„ฑ์ž

public StringBuffer(int length){
    value = new char[length];
    shared = false;
}
public StringBuffer(){
    this(16);
}
public StringBuffer(String str){
    this(str.length()+16);
    append(str)
}

StringBuffer ์ธ์Šคํ„ด์Šค์— ์ €์žฅ๋  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ถฉ๋ถ„ํžˆ ์—ฌ์œ ์žˆ๋Š” ํฌ๊ธฐ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด 16๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์˜ ๋ฒ„ํผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

String ํด๋ž˜์Šค์™€ ์œ ์‚ฌํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฌธ์ž ์ถ”๊ฐ€, ๋ณ€๊ฒฝ, ์‚ญ์ œ์™€ ๊ฐ™์ด ์ €์žฅ๋œ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋„ ๊ฐ–๋Š”๋‹ค.

3. StringBuilder

StringBuffer๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ์— ์•ˆ์ „ํ•˜๋„๋ก ๋™๊ธฐํ™” ๋˜์–ด ์žˆ๊ณ  ๋™๊ธฐํ™”๊ฐ€ ์„ฑ๋Šฅ์„ ์ €ํ•˜ ์‹œํ‚จ๋‹ค.
๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์„ฑ๋œ ํ”„๋กœ๊ทธ๋žจ์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ๋ถˆํ•„์š”ํ•œ ๋™๊ธฐํ™”๋Š” ์„ฑ๋Šฅ ์ €ํ•˜๋งŒ ์œ ๋ฐœํ•˜๊ฒŒ ๋œ๋‹ค.

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

ex) ์‚ฌ์šฉ์˜ˆ์‹œ

// StringBuffer
StringBuffer sb = new StringBuffer();
sb.append("abc");

// StringBuilder
StringBuilder sb = new StringBuilder();
sb.append("abc");

4. ์„ธ๊ฐ€์ง€์— ๋Œ€ํ•œ ๋น„๊ต

4-1. String๊ณผ StringBuffer

String์€ ๋ถˆ๋ณ€์„ฑ <-> StringBuffer์€ ๊ฐ€๋ณ€์„ฑ
์ฃผ์š”ํ•œ ํฌ์ธํŠธ๋Š” ์ƒ์„ฑ ์ดํ›„ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋ƒ ์—†๋ƒ์ด๋‹ค.

String์€ new ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋ฉด ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ์ ˆ๋Œ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๊ทธ๋ž˜์„œ + ์—ฐ์‚ฐ์ด๋‚˜ concat์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์— ๋ณ€ํ™”๋ฅผ ์ค˜๋„ ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค.
์ด๋ ‡๊ฒŒ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์ด ๋งŒ๋“ค์–ด์ง€๋ฉด ๊ธฐ์กด์˜ ๋ฌธ์ž์—ด์€ ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰ํ„ฐ์— ์˜ํ•ด ์ œ๊ฑฐ๋˜์•ผ ํ•˜๋Š” ๋‹จ์ (์–ธ์ œ ์ œ๊ฑฐ๋ ์ง€ ๋ชจ๋ฆ„)์ด ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ๋งŽ์•„์งˆ ๋•Œ ๊ณ„์†ํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ๋ฐ–์— ์—†๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

๋Œ€์‹  String ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋Š” ๋ถˆ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํ•˜๊ฒŒ ์ฝ์–ด๊ฐ€๋Š” ์กฐํšŒ์—ฐ์‚ฐ์—์„œ๋Š” ํƒ€ ํด๋ž˜์Šค๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๊ณ   
๋ถˆ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œํ™˜๊ฒฝ์—์„œ ๋™๊ธฐํ™”๋ฅผ ์‹ ๊ฒฝ์“ธ ํ•„์š”๊ฐ€ ์—†๊ธฐ๋„ ํ•˜๋‹ค.  

๊ฒฐ๋ก ์ ์œผ๋กœ String ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ์ ๊ณ  ์กฐํšŒ๊ฐ€ ๋งŽ์„ ๋•Œ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ๋งŽ์€ ๊ฒฝ์šฐ์—๋Š” StringBuffer์‚ฌ์šฉ์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.

4-2. StringBuffer์™€ StringBuilder

์ฃผ์š”ํ•œ ํฌ์ธํŠธ๋Š” ๋™๊ธฐํ™” ์—ฌ๋ถ€์ด๋‹ค.

StringBuffer๋Š” ๊ฐ ๋ฉ”์„œ๋“œ๋ณ„๋กœ Synchronized Keyword๊ฐ€ ์กด์žฌํ•˜์—ฌ, ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ๋„ ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ
StringBuilder๋Š” ๋™๊ธฐํ™”๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋”ฐ๋ผ์„œ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์ด๋ผ๋ฉด ๊ฐ’ ๋™๊ธฐํ™” ๋ณด์žฅ์„ ์œ„ํ•ด StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ,
๋‹จ์ผ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์ด๋ผ๋ฉด StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๋‹จ์ผ ์Šค๋ ˆ๋“œํ™˜๊ฒฝ์—์„œ StringBuffer๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋™๊ธฐํ™” ๊ด€๋ จ ์ฒ˜๋ฆฌ๋กœ ์ธํ•ด StringBuilder์— ๋น„ํ•ด ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š๋‹ค.


Reference & Additional Resources