[Java] String, StringBuffer, StringBuilder ์ฐจ์ด์ 

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

String Pool?

public static void main(String[] args){
    String s1 = "Cat";
    String s2 = "Cat";
    String s3 = new String("Cat");
    
    System.out.println(s1==s2); // true
    System.out.println(s1==s3); // false
                    
}

์ฒซ๋ฒˆ์งธ๋Š” String Pool์— ์กด์žฌํ•˜๊ฒŒ ๋˜๊ณ , ๋‘๋ฒˆ์งธ new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด String pool์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋™์ผ์„ฑ(identity) ๋น„๊ต์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Journal Dev ๋ฐœ์ทŒ

์™œ String Pool์„ ์‚ฌ์šฉํ• ๊นŒ? Connection Pool, Thread Pool, ๋ชจ๋“  ํ’€์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋‹น์—ฐํžˆ ์žฌํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค.

String ํด๋ž˜์Šค์˜ intern๋ฉ”์„œ๋“œ๋Š”?

if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

๋งŒ์•ฝ์— String pool์— ์กด์žฌํ•˜๋ฉด pool์—์„œ ๋ฐ”๋กœ return ํ•˜๊ณ , String pool์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด pool์— ์ถ”๊ฐ€ํ•˜๊ณ , String ๊ฐ์ฒด์˜ ์ฐธ์กฐ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์˜ˆ์ œ์—์„œ s1 == s3.intern() ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๊ฒŒ๋˜๋ฉด true๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

String์€ Immutable ๋ถˆ๋ณ€ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

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

๋งŒ์•ฝ์— ์—„์ฒญ๋‚œ ์ˆ˜์˜ ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์‹ญ์‹œ์š”. ์‚ฌ์šฉํ•˜์ง€๋„ ์•Š๋Š” ํž™ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ๋“ค์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๊ณ (๋‚˜์ค‘์— GC์— ์˜ํ•ด ์ˆ˜๊ฑฐํ•˜๋”๋ผ๋„) ๋ฉ”๋ชจ๋ฆฌ Leak์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž๋ฐ”์—์„œ๋Š” StringBuffer, StringBuilder๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

StringBuilder, StringBuffer ๋Š” mutable ํ•ฉ๋‹ˆ๋‹ค.

StringBuffer, StringBuilder๋Š” String๊ณผ ๋‹ค๋ฅด๊ฒŒ mutable ํ•œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. append(), insert(), delete() and substring() ์ด๋Ÿฐ ๋ฉ”์„œ๋“œ๋“ค์„ ํ†ตํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ƒˆ๋กญ๊ฒŒ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๊ฐ€๋ณ€์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Š˜๋ ค๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

StringBuffer vs StringBuilder์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š” ?

StringBuffer๋Š” ๋ชจ๋“  public ๋ฉ”์„œ๋“œ์— syncronized ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋ง์ธ ์ฆ‰์Šจ, Thread-safeํ•˜๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ๋™์‹œ์— Thread-safeํ•˜๋‹ค๋Š” ๋ง์€ ์ž์›์„ ์„ ์ ํ• ๋•Œ Lock๋ฅผ ๊ฑธ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋งŒํผ์˜ ์„ฑ๋Šฅ ํšจ์œจ์ด ๋งค์šฐ ์•ˆ ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 1.4๋•Œ๊นŒ์ง€๋Š” StringBuffer๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ๊ทธ ์ดํ›„ ๋ฒ„์ „์—๋Š” StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. StringBuilder๋Š” ๊ทธ์— ๋ฐ˜ํ•ด ๋‹น์—ฐํžˆ ์„ฑ๋Šฅ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์ •๋ฆฌ

์—ฐ๊ด€ ํฌ์ŠคํŠธ