String πŸ†š StringBuffer πŸ†š StringBuilder

nacSeo (λ‚™μ„œ)Β·2022λ…„ 11μ›” 23일
0

지식저μž₯μ†Œ

λͺ©λ‘ 보기
9/34

길이

  • String : κ³ μ •λœ 길이, λΆˆλ³€ β†’ 객체 λ³€κ²½ μ‹œ μ‹€μ œλ‘œ λ³€κ²½λ˜λŠ” 게 μ•„λ‹ˆλΌ μƒˆλ‘œμš΄ String객체가 생성됨
  • StringBuffer : 길이 λ³€κ²½ κ°€λŠ₯❗️
  • StringBuilder : 길이 λ³€κ²½ κ°€λŠ₯❗️

λ©€ν‹°μŠ€λ ˆλ“œ ν™˜κ²½ - thread-safe

  • Thread-safe
    : λ©€ν‹°μŠ€λ ˆλ“œ ν™˜κ²½μ—μ„œ μ–΄λ–€ λ©”μ„œλ“œλ‚˜ λ³€μˆ˜, 객체에 λ™μ‹œμ ‘κ·Όν•΄λ„ λ¬Έμ œκ°€ μ—†λŠ” 것, ν•˜λ‚˜μ˜ ν•¨μˆ˜μ— μŠ€λ ˆλ“œκ°€ λ™μ‹œμ— ν˜ΈμΆœν•˜λ”λΌλ„ μ‹€ν–‰ κ²°κ³Όκ°€ λ°”λ₯΄κ²Œ λ‚˜μ˜€λŠ” 것
  • StringBuffer : thread-safe ⭕️, λ™μ‹œμ— ν•΄λ‹Ή λ¬Έμžμ—΄μ— μ ‘κ·Όν•  λ•Œ 유용
  • StringBuilder : μœ„ 상황이 아닐 λ•ŒλŠ” Builderλ₯Ό μ‚¬μš©ν•˜λŠ” 게 μ„±λŠ₯ 상 유리
    β€» μ„±λŠ₯ ∝ thread-safe (λ°˜λΉ„λ‘€)

정리

  • String : λ¬Έμžμ—΄ 변경이 λΉˆλ²ˆν•˜μ§€ μ•Šμ„ λ•Œ μ‚¬μš©
  • StringBuffer : λ¬Έμžμ—΄μ΄ λΉˆλ²ˆν•˜κ²Œ λ³€κ²½λ˜λ©΄μ„œ, λ©€ν‹°μŠ€λ ˆλ“œ ν™˜κ²½μΌ λ•Œ μ‚¬μš©
  • StringBuilder : λ¬Έμžμ—΄μ΄ λΉˆλ²ˆν•˜κ²Œ λ³€κ²½λ˜λ©΄μ„œ, λ©€ν‹°μŠ€λ ˆλ“œ ν™˜κ²½μ΄ 아닐 λ•Œ μ‚¬μš©

StringBuffer / StringBuilder 생성

  1. κΈ°λ³Έ μƒμ„±μž
StringBuffer sb1 = new StringBuffer();
StringBuilder sb2 = new StringBuilder();
  1. int size값을 인자둜 ν•˜λŠ” μƒμ„±μž : intκ°’μœΌλ‘œ buffer size(capacity) 지정
StringBuffer sb1 = new StringBuffer(10);
StringBuilder sb2 = new StringBuilder(10);
  1. String λ¬Έμžμ—΄μ„ 인자둜 ν•˜λŠ” μƒμ„±μž
StringBuffer sb1 = new StringBuffer("hello");
StringBuilder sb2 = new StringBuilder("hello");

StringBuffer / StringBuilder λ©”μ„œλ“œ

  • append() : λ¬Έμžμ—΄ μΆ”κ°€ (문자 맨 λ’€ μ‚½μž…)
  • length() : λ¬Έμžμ—΄ 길이 λ°˜ν™˜
  • capacity() : ν• λ‹Ήλœ capacity λ°˜ν™˜
  • insert() : 인자둜 μ‹œμž‘ μΈλ±μŠ€μ™€ μ‚½μž…ν•  λ¬Έμžμ—΄μ„ λ°›μ•„ μ›ν•˜λŠ” μœ„μΉ˜μ— λ¬Έμžμ—΄ μ‚½μž…
  • reverse() : λ¬Έμžμ—΄μ„ 뒀집을 λ•Œ μ‚¬μš©
  • delete() : 인자둜 μ‹œμž‘μΈλ±μŠ€μ™€ μ’…λ£ŒμΈλ±μŠ€λ₯Ό λ°›μ•„ μ‹œμž‘μΈλ±μŠ€λΆ€ν„° (μ’…λ£ŒμΈλ±μŠ€-1)κΉŒμ§€ 문자 μ‚­μ œ
  • deleteCharAt() : n번 μ§Έ 인덱슀 ν•œ 문자만 μ‚­μ œ
  • replace() : 인자둜 μ‹œμž‘μΈλ±μŠ€, μ’…λ£ŒμΈλ±μŠ€, λ¬Έμžμ—΄μ„ λ°›μ•„ μ‹œμž‘μΈλ±μŠ€λΆ€ν„° (μ’…λ£ŒμΈλ±μŠ€-1)κΉŒμ§€ κΈ°μ‘΄ λ¬Έμžμ—΄μ„ 인자둜 받은 λ¬Έμžμ—΄λ‘œ μΉ˜ν™˜
profile
λ°±μ—”λ“œ 개발자 κΉ€μ°½ν•˜μž…λ‹ˆλ‹€ πŸ™‡β€β™‚οΈ

0개의 λŒ“κΈ€