Responsive Advertisement

Java String Concatenation: ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์˜ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•

Java์—์„œ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ž‘์—…์€ ๋งค์šฐ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ(String Concatenation)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์—ด์„ ํ•ฉ์น˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด๊ณผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฒฐํ•ฉํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Java์—์„œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๊ณผ ์„ฑ๋Šฅ์ƒ์˜ ๊ณ ๋ ค์‚ฌํ•ญ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•

Java์—์„œ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ด ์™ธ์—๋„ concat() ๋ฉ”์„œ๋“œ, StringBuilder, StringBuffer์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1.1 + ์—ฐ์‚ฐ์ž

+ ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด๊ณผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•  ๋•Œ๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ

public class StringPlusOperator {
    public static void main(String[] args) {
        String firstName = "John";
        String lastName = "Doe";
        String fullName = firstName + " " + lastName;

        System.out.println("Full Name: " + fullName);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

Full Name: John Doe

์œ„ ์˜ˆ์ œ์—์„œ + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด firstName๊ณผ lastName์„ ์—ฐ๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

1.2 concat() ๋ฉ”์„œ๋“œ

concat() ๋ฉ”์„œ๋“œ๋Š” String ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ๋‘ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, null ๊ฐ’์„ ํฌํ•จํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ

public class StringConcatMethod {
    public static void main(String[] args) {
        String greeting = "Hello";
        String target = "World";
        String message = greeting.concat(", ").concat(target).concat("!");

        System.out.println("Message: " + message);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

Message: Hello, World!

concat() ๋ฉ”์„œ๋“œ๋Š” + ์—ฐ์‚ฐ์ž์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜์ง€๋งŒ, ์—ฐ์‚ฐ ๊ธฐํ˜ธ๋ณด๋‹ค ๋ช…์‹œ์ ์ด๋ผ๋Š” ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1.3 StringBuilder์™€ StringBuffer

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

StringBuilder ์˜ˆ์ œ

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append(", ").append("World").append("!");

        System.out.println("StringBuilder Result: " + sb.toString());
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

StringBuilder Result: Hello, World!

StringBuffer ์˜ˆ์ œ

public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(", ").append("World").append("!");

        System.out.println("StringBuffer Result: " + sb.toString());
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

StringBuffer Result: Hello, World!

์œ„ ๋‘ ํด๋ž˜์Šค๋Š” ๋Œ€๋Ÿ‰์˜ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ํšจ์œจ์ ์ด๋ฉฐ, ํŠนํžˆ ๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•  ๋•Œ ์„ฑ๋Šฅ์ƒ์˜ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

2. ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์„ฑ๋Šฅ ๋น„๊ต

๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ์„ฑ๋Šฅ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๊ฐ ๋ฐฉ๋ฒ•์˜ ์„ฑ๋Šฅ ํŠน์ง•์ž…๋‹ˆ๋‹ค:

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

์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ ์˜ˆ์ œ

public class StringConcatenationPerformance {
    public static void main(String[] args) {
        long startTime, endTime;

        // + ์—ฐ์‚ฐ์ž
        startTime = System.currentTimeMillis();
        String result = "";
        for (int i = 0; i < 10000; i++) {
            result += i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("+ ์—ฐ์‚ฐ์ž ์‹œ๊ฐ„: " + (endTime - startTime) + "ms");

        // StringBuilder
        startTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            sb.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder ์‹œ๊ฐ„: " + (endTime - startTime) + "ms");
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

+ ์—ฐ์‚ฐ์ž ์‹œ๊ฐ„: 400ms
StringBuilder ์‹œ๊ฐ„: 5ms

์œ„ ํ…Œ์ŠคํŠธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•  ๋•Œ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

3. ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

  • ๋ฐ˜๋ณต๋ฌธ์—์„œ + ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ ์ง€์–‘: ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด StringBuilder ๋˜๋Š” StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
  • ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์ง€: ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์ž‘์—…์ด ๋งŽ์€ ๊ฒฝ์šฐ, ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์ตœ์†Œํ™”ํ•˜์„ธ์š”.
  • ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์‹œ ๋™๊ธฐํ™” ๊ณ ๋ ค: ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ๋Š” StringBuffer๋ฅผ ์‚ฌ์šฉํ•ด ๋™๊ธฐํ™”๋ฅผ ๋ณด์žฅํ•˜์„ธ์š”.

๊ฒฐ๋ก 

Java์—์„œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ž‘์—…์˜ ๋ณต์žก๋„์™€ ์‚ฌ์šฉ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์ ํ•ฉํ•œ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์—ฐ๊ฒฐ ์ž‘์—…์—๋Š” + ์—ฐ์‚ฐ์ž๋‚˜ concat() ๋ฉ”์„œ๋“œ๋ฅผ, ๋Œ€๊ทœ๋ชจ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋‚˜ ๋ฐ˜๋ณต์ ์ธ ์—ฐ๊ฒฐ ์ž‘์—…์—๋Š” StringBuilder ๋˜๋Š” StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ ์†Œ๊ฐœํ•œ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•ด ํšจ์œจ์ ์ธ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”!

๋Œ“๊ธ€ ์“ฐ๊ธฐ