Skip to content

Latest commit

ย 

History

History
29 lines (22 loc) ยท 1.98 KB

File metadata and controls

29 lines (22 loc) ยท 1.98 KB

String vs StringBuffer vs StringBuilder

String StringBuffer StringBuilder
์ €์žฅ ๊ณต๊ฐ„ String pool Heap Heap
์ˆ˜์ • ๊ฐ€๋Šฅ ์—ฌ๋ถ€ X(Immutable) O(Mutable) O(Mutable)
Thread Safe O O X
๋™๊ธฐํ™” Synchromized O O X
์„ฑ๋Šฅ ๋น ๋ฆ„ ๋Š๋ฆผ ๋น ๋ฆ„

String ๊ฐ์ฒด๋Š” ํ•œ๋ฒˆ ๊ฐ’์ด ํ• ๋‹น๋˜๋ฉด ๊ทธ ๊ณต๊ฐ„์€ ๋ณ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
+ ์—ฐ์‚ฐ์ž ๋˜๋Š” concat ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ ๊ธฐ์กด ๋ฌธ์ž์—ด์— ๋ถ™์ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ,
์ƒˆ๋กœ์šด String ๊ฐ์ฒด๋ฅผ ๋งŒ๋“  ํ›„ ์ƒˆ String ๊ฐ์ฒด์— ์—ฐ๊ฒฐ๋œ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ๊ทธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰ String ํด๋ž˜์Šค ๊ฐ์ฒด๋Š” Heap ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ƒ์„ฑ๋˜๊ณ  ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๋‚ด์šฉ์„ ๋ณ€ํ™”์‹œํ‚ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
์ฃผ๋กœ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ์ ๊ณ  ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์ผ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

StringBuffer

์ฃผ๋กœ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ๋งŽ๊ณ  ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์ผ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
StringBuffer๋Š” ๊ฐ€๋ณ€์„ฑ์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— append(), delete()์™€ ๊ฐ™์€ API๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ผ ๊ฐ์ฒด ๋‚ด ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์—ฌ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „(thread-safe)ํ•˜๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

StringBuilder

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