๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[์šฉ์–ด][Java] ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด String Literal

์ถœ์ฒ˜ 

ChatGPT

๋„ค์ด๋ฒ„์‚ฌ์ „


Literal์˜ ์˜์–ด ์˜๋ฏธ 

๋„ค์ด๋ฒ„ ์˜์–ด์‚ฌ์ „ ๋™์•„์ถœํŒ Literal ์˜๋ฏธ

Literal์€ '์ƒ์ˆ˜' ์ •๋„๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋  ๋“ฏํ•˜๋‹ค.

 

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด String Literal

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ฝ”๋“œ ๋‚ด์— ์ง์ ‘ ์ž‘์„ฑ๋œ ๋ฌธ์ž์—ด ๊ฐ’์„ ๋งํ•œ๋‹ค. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ฝ”๋“œ์—์„œ ๊ณ ์ •๋œ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰๋˜๋ฉฐ, ์ฃผ๋กœ ํฐ๋”ฐ์˜ดํ‘œ("")๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ƒ์ˆ˜ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰๋œ๋‹ค.

ํŠน์ง•

๋ถˆ๋ณ€์„ฑ

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ถˆํŽธ(immutable)์ด๋‹ค. ์ฆ‰, ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ

์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์ŠคํŠธ๋ง ํ’€(String Pool)์ด๋ผ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ €์žฅํ•œ๋‹ค. ๋™์ผํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉ๋˜๋ฉด, ์ž๋ฐ”๋Š” ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.

๊ณ ์ •๋œ ๊ฐ’

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์— ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฐ’์œผ๋กœ, ์‹คํ–‰ ์‹œ์ ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

 

์˜ˆ์‹œ

public class StringLiteralExample {
    public static void main(String[] args) {
        // ๊ฐ„๋‹จํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด
        String hello = "Hello, World!";
        
        // ์œ ๋‹ˆ์ฝ”๋“œ ์ด์Šค์ผ€์ดํ”„๋ฅผ ํฌํ•จํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด
        String unicodeString = "This is a unicode snowman: \u2603";

        // ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด (Java 13+)
        String multiLine = """
            This is a multi-line
            string literal
            in Java.
            """;

        // ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด
        String specialChars = "This string contains a tab\tand a newline\n";

        // ์ถœ๋ ฅ
        System.out.println(hello);
        System.out.println(unicodeString);
        System.out.println(multiLine);
        System.out.println(specialChars);
    }
}

 

์ถœ๋ ฅ

Hello, World!
This is a unicode snowman: โ˜ƒ
This is a multi-line
string literal
in Java.
This string contains a tab    and a newline

 

๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ : ์ŠคํŠธ๋ง ํ’€ (String Pool)

์ž๋ฐ”์—์„œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ŠคํŠธ๋ง ํ’€(String Pool)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ €์žฅ๋œ๋‹ค. ์ด ์˜์—ญ์€ JVM์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋ฉฐ, ๋™์ผํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ƒ์„ฑ๋˜๋”๋ผ๋„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•ด ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.

 

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

public class StringPoolExample {
    public static void main(String[] args) {
        // ๋‘ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ์„ ์–ธ
        String str1 = "Hello";
        String str2 = "Hello";

        // ์ฐธ์กฐ ๋น„๊ต
        if (str1 == str2) {
            System.out.println("str1 and str2 refer to the same object in the string pool.");
        } else {
            System.out.println("str1 and str2 refer to different objects.");
        }
    }
}

 

์ถœ๋ ฅ

str1 and str2 refer to the same object in the string pool.

 

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ถˆ๋ณ€์„ฑ

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ๋ถˆ๋ณ€์ด๋‹ค. ์ฆ‰, ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•˜๋Š” ์ž‘์—…์€ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ด๋Š” ๋ถˆ๋ณ€ ๊ฐ์ฒด์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์Šค๋ ˆ๋“œ ์•ˆ์ •์„ฑ์„ ์ œ๊ณตํ•˜๊ณ , ํ•ด์‹œ ์ฝ”๋“œ ์บ์‹ฑ ๋ฐ ๊ธฐํƒ€ ์ตœ์ ํ™”๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.

public class StringImmutabilityExample {
    public static void main(String[] args) {
        String original = "Java";
        String modified = original.concat(" Programming");

        // ์ถœ๋ ฅ
        System.out.println("Original: " + original);
        System.out.println("Modified: " + modified);
    }
}

 

์ถœ๋ ฅ

Original: Java
Modified: Java Programming

 

์œ„ ์ฝ”๋“œ์—์„œ 'original' ๋ฌธ์ž์—ด์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ , 'modified' ๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋œ๋‹ค.

 

ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์—๋Š” ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•ด ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • \n : ์ƒˆ ์ค„(new line)
  • \t : ํƒญ(tab)
  • \\ : ๋ฐฑ์Šฌ๋ž˜์‹œ(backslash)
  • \" : ํฐ๋”ฐ์˜ดํ‘œ(double quote)
  • \' : ์ž‘์€๋”ฐ์˜ดํ‘œ(single quote)

์˜ˆ์‹œ ์ฝ”๋“œ

public class SpecialCharExample {
    public static void main(String[] args) {
        String text = "First line\nSecond line\tIndented";
        System.out.println(text);
    }
}

 

 

์ถœ๋ ฅ

First line
Second line	Indented

 

๊ฒฐ๋ก 

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ž๋ฐ” ๋ฐ ๋‹ค๋ฅธ ๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ๊ณ ์ •๋œ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ์ค‘์š”ํ•œ ๋ฐฉ์‹์ด๋‹ค. ์ด๋Ÿฌํ•œ ๋ฆฌํ„ฐ๋Ÿด์€ ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ€์ง€๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์ŠคํŠธ๋ง ํ’€์„ ํ†ตํ•ด ๊ด€๋ฆฌ๋œ๋‹ค. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํ™œ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ ๋‚ด์—์„œ ์ง์ ‘์ ์œผ๋กœ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ณ , ๋‹ค์–‘ํ•œ ํ˜•์‹์˜ ํ…์ŠคํŠธ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.