๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JAVA ๊ฐœ๋ฐœ์ž๊ฐ€ ์•Œ์•„์•ผํ•  String ํด๋ž˜์Šค - 2ํŽธ

by Leica 2023. 2. 18.
๋ฐ˜์‘ํ˜•

 

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ง€๋‚œ ํฌ์ŠคํŒ…์— ์ด์–ด์„œ JAVA ๊ฐœ๋ฐœ์ž๊ฐ€ ์•Œ์•„์•ผํ•˜๋Š” String ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฃผ์š” ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. String, StringBuilder, StringBuffer์˜ ํŠน์ง•๊ณผ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์ฃผ๋กœ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

 

๋ชฉ์ฐจ

  • String, StringBuilder, StringBuffer - immutable(๋ถˆ๋ณ€) VS mutable(๊ฐ€๋ณ€)
  • StringBuilder VS StringBuffer

 


 

String, StringBuilder, StringBuffer - immutable(๋ถˆ๋ณ€) VS mutable(๊ฐ€๋ณ€)

String, StringBuilder, StringBuffer๋Š” ๋ชจ๋‘ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๊ฐ ํด๋ž˜์Šค๋Š” ๊ณ ์œ ํ•œ ํŠน์„ฑ์ด ์žˆ์–ด ์ด๋ฅผ ์•Œ๊ณ  ์ ํ•ฉํ•œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. String ๊ฐ์ฒด์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๊ฐ€ immutable(๋ถˆ๋ณ€)ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ์— ๋ฐ˜ํ•ด StringBuilder์™€ StringBuffer ํด๋ž˜์Šค๋Š” mutable(๊ฐ€๋ณ€)ํ•ฉ๋‹ˆ๋‹ค. String ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๊ฐ€ ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€๋งŒ StringBuilder์™€ StringBuffer๋Š” ๊ธฐ์กด ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. 

 

๐Ÿ“’ String ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋ฉด ๊ธฐ์กด ๊ฐ์ฒด๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์ด์ „ ๋ฌธ์ž์—ด ๊ฐ์ฒด๋Š” Garbage Collector์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ๋ฌธ์ž์—ด์„ ์ž์ฃผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„์™€ ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ StringBuilder/StringBuffer ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ์ž์ฃผ ๋ณ€๊ฒฝํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

 

๋‹ค์Œ ์ฝ”๋“œ๋Š” StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  append() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
String str = sb.toString();

StringBuilder์˜ append() ๋ฉ”์†Œ๋“œ๋Š” ๊ธฐ์กด ๋ฌธ์ž์—ด์— ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  toString() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ StringBuffer๋‚˜ StringBuilder์˜ ๊ฐ์ฒด๋ฅผ String์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ, StringBuilder ๊ฐ์ฒด sb๋Š” "Hello" ๋ฌธ์ž์—ด์„ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. append() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ " world" ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๋ฉด sb์˜ ๋‚ด์šฉ์ด "Hello world"๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, toString() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ sb๋ฅผ String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“’ StringBuilder/StringBuffer์˜ String ๋ณ€ํ™˜
StringBuffer๋‚˜ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฌธ์ž์—ด ์—ฐ์‚ฐ ๊ณผ์ •์—์„œ ๋งŽ์€ ๋ฌธ์ž์—ด์ด ์ž„์‹œ๋กœ ์ƒ์„ฑ๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฌธ์ž์—ด ์—ฐ์‚ฐ ๊ณผ์ •์—์„œ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์ด String ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, toString()์„ ํ˜ธ์ถœํ•˜์—ฌ String์œผ๋กœ ๋ณ€ํ™˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. 
ํ•˜์ง€๋งŒ, StringBuffer๋‚˜ StringBuilder๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋“œ์‹œ String ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด, toString() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ String์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋” ์ด์ƒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ค๊ณ , ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๋‚ด๋ถ€ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜์—ฌ ์ตœ์ข… ๋ฌธ์ž์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ตœ์ ํ™”์— ์ด์ ์„ ์ค๋‹ˆ๋‹ค.

 

append() ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฐ์†ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์ฝ”๋“œ๋Š” StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "Hello", "world", "!" ๋ฌธ์ž์—ด์„ ์ฐจ๋ก€๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

StringBuilder sb = new StringBuilder("Hello");
sb.append(" world").append("!");
System.out.println(sb.toString()); // "Hello world!"

์œ„ ์ฝ”๋“œ์—์„œ๋Š” append() ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฐ์†ํ•ด์„œ ์‚ฌ์šฉํ•˜์—ฌ "Hello", " world", "!" ๋ฌธ์ž์—ด์„ ์ฐจ๋ก€๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

๐Ÿ“’ ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹(method chaining)
์—ฐ์†ํ•ด์„œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹(method chaining)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ€๋…์„ฑ๋„ ์ข‹์•„์ง‘๋‹ˆ๋‹ค.

 

StringBuilder VS StringBuffer

StringBuilder์™€ StringBuffer๋Š” ๋‘˜ ๋‹ค ๊ฐ€๋ณ€์ ์ธ ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๋‘ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋™๊ธฐํ™”(synchronization)์™€ Thread-Safety

๋‘˜์˜ ์ฐจ์ด์ ์€ ๋™๊ธฐํ™”(synchronization)์˜ ์ง€์› ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. StringBuilder๋Š” ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋น„๋™๊ธฐํ™” ํด๋ž˜์Šค์ด๋ฉฐ, StringBuffer๋Š” ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. StringBuilder๋Š” non-synchronized๋กœ, StringBuffer๋Š” synchronized๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์–ด์„œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ StringBuffer๋Š” ์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ์„ ๋ณด์žฅํ•˜๊ณ , StringBuilder๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ์—์„œ ๋ฌธ์ž์—ด ์กฐ์ž‘์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

 

์„ฑ๋Šฅ

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

Java ๋ฒ„์ „

StringBuilder๋Š” Java 5๋ถ€ํ„ฐ ์ œ๊ณต๋˜๋ฉฐ, StringBuffer๋Š” Java 1.0๋ถ€ํ„ฐ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

 

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

 

ํ—ˆ์ „ํ•ด์„œ ๋ถ™์—ฌ๋ณด๋Š” ์ด๋ฏธ์ง€...

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€