โšก Java.lang.StringBuffer & StringBuilder

sonjh919ยท2023๋…„ 10์›” 18์ผ
0

Java API

๋ชฉ๋ก ๋ณด๊ธฐ
5/16
post-thumbnail

๐Ÿ“Œ StringBuffer & StringBuilder ํด๋ž˜์Šค

  • ๊ฐ€๋ณ€ ํด๋ž˜์Šค์ธ StringBuffer ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฒ„ํผ(buffer)๋ผ๋Š” ๋…๋ฆฝ์ ์ธ ๊ณต๊ฐ„์„ ๊ฐ€์ง„๋‹ค.
  • ๋ฒ„ํผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๋„ ์—†๊ณ , ์†๋„๋„ String์— ๋น„ํ•ด์„œ ๋งค์šฐ ๋น ๋ฅด๋‹ค.

๐Ÿ’ก ๊ฐ€๋ณ€ ํด๋ž˜์Šค(mutable class)
StringBuffer ์ธ์Šคํ„ด์Šค๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๐Ÿ’ก ๋ฒ„ํผ(buffer)
๋ฒ„ํผ ํฌ๊ธฐ์˜ ๊ธฐ๋ณธ๊ฐ’์€ 16๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์ด๋‹ค.
์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ทธ ํฌ๊ธฐ๋ฅผ ๋ณ„๋„๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๐ŸŒˆ StringBuffer

๋ฉ”์†Œ๋“œ์„ค๋ช…
StringBuffer toString()StringBuffer๋ฅผ String ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
StringBuffer append(String str)์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„, ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•จ.
StringBuffer delete(int start, int end)์ „๋‹ฌ๋œ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ์ œ๊ฑฐํ•จ.**
StringBuffer deleteCharAt(int index)์ „๋‹ฌ๋œ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ์ œ๊ฑฐํ•จ.
StringBuffer insert(int offset, String str)ํŠน์ • ์œ„์น˜์— ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•จ.
StringBuffer reverse()ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์žฌ๋ฐฐ์—ดํ•จ.
StringBuffer substring(int start, int end)StringBuffer ๊ฐ์ฒด์˜ ์‹œ์ž‘ ์œ„์น˜์—์„œ ๋ ์œ„์น˜๊นŒ์ง€์˜ ๋ฌธ์ž๋ฅผ ๋ฝ‘์•„๋‚ธ๋‹ค.
int capacity()ํ˜„์žฌ ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ.

๐ŸŒˆ StringBuilder

  • StringBuffer๋Š” multi-thread ํ™˜๊ฒฝ์— ์ ํ•ฉํ•˜์ง€๋งŒ, StringBuilder๋Š” single-thread ํ™˜๊ฒฝ์— ์ ํ•ฉํ•˜๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€