Programming/Java

F004 - Reference Type

osean 2021. 5. 8. 02:55
โœ๏ธ ๋“œ๋””์–ด ์ฒซ ๋ฉ˜ํ† ๋ง์„ ์‹œ์ž‘ํ–ˆ๋‹ค! ์—ฌ๋Ÿฌ ๋Œ€ํ™”๊ฐ€ ์˜ค๊ฐ”์ง€๋งŒ, ๊ฐ€์žฅ ์ž์‹  ์—†์—ˆ๋˜ ์ฐธ์กฐ ํƒ€์ž…๊ณผ ํ•ด๋‹น ํƒ€์ž…์ด ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ์–ด๋–ป๊ฒŒ ์šด์šฉ๋˜๋Š”์ง€ ๊ธฐ๋กํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์™œ๋ƒ๋ฉด ๋ฉ˜ํ† ๋‹˜๊ป˜์„œ ํ•ด๋‹น ์งˆ๋ฌธ์„ ์ฃผ์…จ๋Š”๋ฐ ๋Œ€๋‹ต์„ ์ž˜๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์‹œ ๋˜์งš์–ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ง€๋ ค๊ณ  ํ•œ๋‹ค.

์ฐธ์กฐ ์ž๋ฃŒํ˜•

์ฐธ์กฐ ์ž๋ฃŒํ˜•์€ ์•ž์„œ ๊ณต๋ถ€ํ•œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ž๋ฃŒํ˜•์„ ๋œปํ•œ๋‹ค. ๋‹ค๋งŒ String ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Literal ํ˜•ํƒœ๋กœ ๋ณ€์ˆ˜๊ฐ’์„ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๊ณ , + ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•œ ์œ ์ผํ•œ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์ด๋‹ค.

๋ชจ๋“  ์ฐธ์กฐ ์ž๋ฃŒํ˜•์€ java.lang.Object ๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์ด๋Ÿฌํ•œ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์—๋Š”

  1. Annotation(@์ฃผ์„)
  2. Arrays(๋ฐฐ์—ด)
  3. Class
  4. Enum
  5. Interface

๊ฐ€ ์กด์žฌํ•œ๋‹ค.

์ฐธ์กฐ ์ž๋ฃŒํ˜•๊ณผ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์˜ ์ฐจ์ด๋ฅผ ์‚ดํŽด๋ณด์ž.

 

Java 8 Pocket Guide

Chapter 4. Reference Types Reference types hold references to objects and provide a means to access those objects stored somewhere in memory. The memory locations are irrelevant to programmers. All … - Selection from Java 8 Pocket Guide [Book]

www.oreilly.com

Reference VS Primitive

Reference Type Primitive Type
Unlimited number of reference types, as they are defined by the user.

(๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ •์˜ ์ž๋ฃŒํ˜•)
Consists of boolean and numeric types: charbyteshortintlongfloat, and double.
Memory location stores a reference to the data.

(Stack ๋ฉ”๋ชจ๋ฆฌ์— ํ•ด๋‹น ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ Heap ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋œ ๊ฐ’์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.)
Memory location stores actual data held by the primitive type.

(Stack ๋ฉ”๋ชจ๋ฆฌ์— ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์˜ ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์ €์žฅ๋œ๋‹ค.)
When a reference type is assigned to another reference type, both will point to the same object.

(ํ•œ ์ฐธ์กฐ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ์ฐธ์กฐ ๊ฐ์ฒด์— ํ• ๋‹น๋˜๋ฉด, ๋‘˜ ๋‹ค ๋™์ผํ•œ ์ฐธ์กฐ ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณธ๋‹ค.)
When a value of a primitive is assigned to another variable of the same type, a copy is made.

(ํ•œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฅธ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ํ• ๋‹นํ•˜๋ฉด ๊ฐ™์€ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ๋ฐ”๋ผ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๋ณต์‚ฌ๋ณธ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ๋‹ค.)
When an object is passed into a method, the called method can change the contents of the object passed to it but not the address of the object.

(๊ฐ์ฒด๊ฐ€ ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ๋˜๋ฉด ์ „๋‹ฌ๋œ ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋Š” ๋ฉ”์†Œ๋“œ์—์„œ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฃผ์†Œ๋Š” ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†๋‹ค.)
When a primitive is passed into a method, only a copy of the primitive is passed. The called method does not have access to the original primitive value and therefore cannot change it. The called method can change the copied value.

(๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์ด ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ๋˜๋ฉด ๊ธฐ์กด์˜ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ํ˜ธ์ถœํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฐพ์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†์ง€๋งŒ, ์ „๋‹ฌ๋œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์€ ๊ธฐ์กด์˜ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์˜ ๋ณต์‚ฌ๋ณธ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ๋œ ๋ฉ”์†Œ๋“œ์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ๋‹ค.)

.hashCode()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

  • Java 8 Reference Docs

ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋Š” java.lang.Object์— ์†ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ, ๋ชจ๋“  ์ฐธ์กฐ ์ž๋ฃŒํ˜•์€ Object๋ฅผ ์ƒ์† ๋ฐ›์œผ๋‹ˆ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ Override ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ ๊ฐ์ฒด์˜ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ int ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ์ด ๊ฐ’์€ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’์„ ํ•ด์‹œํ™”ํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์ด๋‹ค.

public class HashCodeTest {

    public String desc;

    public HashCodeTest(String desc) {
        this.desc = desc;
    }

    public static void main(String[] args) {
        HashCodeTest test0 = new HashCodeTest("Constant Pool");
        HashCodeTest test1 = new HashCodeTest(new String("Heap Memory"));
        HashCodeTest test2 = test1;

        System.out.println("test0.hashCode() = " + test0.hashCode());
        System.out.println("test1.hashCode() = " + test1.hashCode());
        System.out.println("test2.hashCode() = " + test2.hashCode());
    }
}
/*
	test0.hashCode() = 1878246837
	test1.hashCode() = 929338653
	test2.hashCode() = 929338653
*/

์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ ๊ฐ์ฒด์˜ ํ•ด์‹œ์ฝ”๋“œ ๊ฐ’์„ ํ™•์ธํ•ด๋ณด์ž. test0๊ณผ test1์€ ๊ฐ™์€ HashCodeTest ํด๋ž˜์Šค์ด์ง€๋งŒ Heap Meory์—๋Š” ๊ฐ์ž ํ• ๋‹น ๋ฐ›๋Š”๋‹ค. test2์˜ ๊ฒฝ์šฐ test1 ๊ฐ์ฒด๋ฅผ ํ• ๋‹น ๋ฐ›์•„ ์ดˆ๊ธฐํ™” ๋˜์—ˆ์œผ๋‹ˆ test1๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์ด .hashCode() ๋ฉ”์†Œ๋“œ๋Š” String ํด๋ž˜์Šค์— ํ•œํ•ด์„œ ๋‹ค๋ฅด๊ฒŒ ์žฌ์ •์˜ ๋˜์–ด ์žˆ๋‹ค.

/**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

String ํด๋ž˜์Šค์— ์žฌ์ •์˜๋œ .hashCode() ์ฝ”๋“œ ๋ธ”๋Ÿญ์ด๋‹ค. ์‚ดํŽด๋ณด๋ฉด ๋ฌธ์ž์—ด ํ•œ ๊ธ€์ž ์”ฉ ๊ฐ€์ ธ์™€ ๊ฐ ์ •์ˆ˜์™€ ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ๋”ํ•œ ์ตœ์ข…๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋•Œ๋ฌธ์— String ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์ค‘๋ณต๋˜๋Š” ํ•ด์‹œ์ฝ”๋“œ๊ฐ€ ์กด์žฌ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ๊ฒฝ์šฐ๋Š” ๋ฌธ์ž์—ด์ด ๊ฐ™์„ ๋•Œ ์กด์žฌํ•œ๋‹ค.

public class StringHashCodeTest {
    public static void main(String[] args) {
        String test0 = "abc";
        String test1 = "abc";
        String test2 = "๊ฐ€๋‚˜๋‹ค";

        System.out.println("test0.hashCode() = " + test0.hashCode());
        System.out.println("test1.hashCode() = " + test1.hashCode());
        System.out.println("test2.hashCode() = " + test2.hashCode());

        String test3 = new String("def");
        String test4 = new String("def");

        System.out.println("test3.hashCode() = " + test3.hashCode());
        System.out.println("test4.hashCode() = " + test4.hashCode());
    }
}
/*
	test0.hashCode() = 96354
	test1.hashCode() = 96354
	test2.hashCode() = 43761996
	test3.hashCode() = 99333
	test4.hashCode() = 99333
*/

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ, test3๊ณผ test4๋Š” Heap Memory์— ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ํ• ๋‹น๋˜์—ˆ๋Š”๋ฐ hashCode์˜ ๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๋‘ ๊ฐ์ฒด์˜ ๊ฐ’(๋ฌธ์ž์—ด)์ด ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

.equals()

ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ๋˜ํ•œ java.lang.Object์— ์†ํ•œ ๋ฉ”์†Œ๋“œ๋กœ, ๋ชจ๋“  ์ฐธ์กฐ ์ฐจ๋ฃŒํ˜•์€ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์žฌ์ •์˜ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋•Œ๋ฌธ์— ๊ฐ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์—์„œ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๊ธฐ์ค€์ด ๋‹ฌ๋ผ์ง„๋‹ค.

.equals() ๋ฉ”์†Œ๋“œ์™€ == ์—ฐ์‚ฐ์ž๋Š” ์„œ๋กœ ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅด๋‹ค. == ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ๊ฐ„ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ ํ•  ๋•Œ๋Š” ๊ฐ’์„ ๋น„๊ตํ•˜์ง€๋งŒ, ์ฐธ์กฐ ์ž๋ฃŒํ˜• ๊ฐ„ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ ํ•  ๋•Œ๋Š” ์ฐธ์กฐ ์ž๋ฃŒํ˜•์˜ ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค. String ํด๋ž˜์Šค์—์„œ .equals() ๋ฉ”์†Œ๋“œ๋Š” ํ•œ ๊ธ€์ž ์”ฉ ๋‚˜๋ˆ„์–ด ๋น„๊ตํ•œ ํ›„ ๋ฌธ์ž์—ด ๊ฐ’์ด ๊ฐ™์€์ง€ ์•„๋‹Œ์ง€ ๋…ผ๋ฆฌ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ ์–ด๋–ค ํด๋ž˜์Šค์—์„œ๋Š” ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ๋” ์ •์˜ ๋‚ด๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค.

String

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ String ํด๋ž˜์Šค๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • new ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•œ ์„ ์–ธ → Heap Memory ์˜์—ญ์— ํ• ๋‹น
String str0 = new String("abcd");
String str1 = new String("abcd");
  • Literal์„ ์ด์šฉํ•œ ์„ ์–ธ → Constant Pool(์ƒ์ˆ˜ ํ’€) ์˜์—ญ์— ํ• ๋‹น
String str2 = "abcd";
String str3 = "abcd";

์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์„ ํ†ตํ•ด ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž์—ด 4๊ฐœ๋ฅผ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์— 4๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ๋ชจ๋‘ ํ• ๋‹น๋ ๊นŒ? HashCode๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž.

public static void main(String[] args) {
        String str0 = new String("abcd");
        String str1 = new String("abcd");
        String str2 = "abcd";
        String str3 = "abcd";

        System.out.println("str0.hashCode() = " + System.identityHashCode(str0));
        System.out.println("str1.hashCode() = " + System.identityHashCode(str1));
        System.out.println("str2.hashCode() = " + System.identityHashCode(str2));
        System.out.println("str3.hashCode() = " + System.identityHashCode(str3));
    }

/*
	str0 = 1163157884
	str1 = 1956725890
	str2 = 356573597
	str3 = 356573597
*/

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ƒ์— 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ํ• ๋‹น๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” Literal๋กœ ์„ ์–ธ๋œ String ๋ณ€์ˆ˜๋Š” Constant Pool์— ํ• ๋‹น๋˜๋ฉฐ, String ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ intern() ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•ด์„œ ๊ฐ€์žฅ ๋จผ์ € Constant Pool์— ํ•ด๋‹น String ๊ฐ’์„ ์ฐพ์•„๋ณธ ํ›„ ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค๋ฉด Constant Pool์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด ๋•Œ, str0 ๋ณ€์ˆ˜์™€ str1 ๋ณ€์ˆ˜๊ฐ€ ๊ฐ’์€ ๊ฐ™์€๋ฐ ์™œ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ฐ์ฒด ์ฃผ์†Œ๋ฅผ ํ• ๋‹น ๋ฐ›์•˜์„๊นŒ? ๊ทธ ์ด์œ ๋Š” ๋‘ ๋ณ€์ˆ˜๊ฐ€ new ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์„ ์–ธ๋œ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— Constant Pool์ด ์•„๋‹Œ Heap Memory ์— ๊ฐ๊ฐ ํ• ๋‹น ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

.intern()

ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋Š” ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ Constant Pool์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ๋Š”๋‹ค. ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋กœ ํ™•์ธํ•ด๋ณด์ž.

public void stringInternMethod() {
        String strA = new String("stringA");
        String strAa = new String("stringA");

        String strB = "stringA";
        String strBa = "stringA";
        String strBb = strB.intern();

        String strC = new String("stringB");
        String strCa = strC.intern();
        String strCb = strC.intern();

        System.out.println("strA = " + System.identityHashCode(strA));
        System.out.println("strAa = " + System.identityHashCode(strAa));

        System.out.println("strB = " + System.identityHashCode(strB));
        System.out.println("strBa = " + System.identityHashCode(strBa));
        System.out.println("strBb = " + System.identityHashCode(strBb));

        System.out.println("strC = " + System.identityHashCode(strC));
        System.out.println("strCa = " + System.identityHashCode(strCa));
        System.out.println("strCb = " + System.identityHashCode(strCb));
    }
/*
	strA = 1163157884
	strAa = 1956725890

	strB = 356573597
	strBa = 356573597
	strBb = 356573597
	
	strC = 1735600054
	strCa = 21685669
	strCb = 21685669
*/

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด strA ์™€ strAa ๋Š” ๊ฐ™์€ ๊ฐ’์ด์ง€๋งŒ ๋‹ค๋ฅธ ์ฃผ์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฑธ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋‘ ๋ณ€์ˆ˜๊ฐ€ new ์ƒ์„ฑ์ž๋กœ ์„ ์–ธ๋˜์–ด Heap Memory์— ๊ฐ๊ฐ ํ• ๋‹น๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฐ˜๋ฉด, strB ๊ด€๋ จ ๋ณ€์ˆ˜๋“ค์„ ์‚ดํŽด๋ณด๋ฉด strB ๋ณ€์ˆ˜๊ฐ€ Literal ๋กœ ์„ ์–ธ๋˜์–ด ํ•ด๋‹น ๊ฐ’์„ Constant Pool์— ํ• ๋‹นํ•œ๋‹ค. ๊ทธ ํ›„ strBa ๋ณ‘์ˆ˜๊ฐ€ Literal๋กœ ์„ ์–ธ๋  ๋•Œ String ํด๋ž˜์Šค ๋‚ด๋ถ€์ ์œผ๋กœ intern()์ด ํ˜ธ์ถœ๋˜์–ด Constant Pool ์—์„œ ํ•ด๋‹น ๊ฐ’์„ ๋จผ์ € ์ฐพ๋Š”๋‹ค. ์ด ๋•Œ ๋จผ์ € ํ• ๋‹น๋œ strB์˜ ๊ฐ’์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— strBa๋Š” ํ•ด๋‹น ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. strBb ๋ณ€์ˆ˜ ๋˜ํ•œ intern() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

strC ๊ด€๋ จ ๋ณ€์ˆ˜๋“ค์„ ์‚ดํŽด๋ณด๋ฉด strC๋Š” new ์ƒ์„ฑ์ž๋กœ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— Heap Memory์— ํ• ๋‹น๋˜์–ด Constant Pool์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋•Œ๋ฌธ์— strCa ๋ณ€์ˆ˜๋Š” intern() ๋ฉ”์†Œ๋“œ๊ฐ€ Constant Pool์—์„œ ํ•ด๋‹น ๊ฐ’์„ ์ฐพ๋‹ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ  Constant Pool์— ํ•ด๋‹น ๊ฐ’์„ ์ƒˆ๋กœ ํ• ๋‹นํ•˜๊ณ  ๊ฐ’์˜ ์ฃผ์†Œ๋ฅผ strCa ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐํ•œ๋‹ค. strCb ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ, strCa ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋˜๋ฉด์„œ Constant Pool์— ๋งŒ๋“ค์–ด ๋†“์€ ๊ฐ’์„ ์ฐพ๊ณ  ํ•ด๋‹น ๊ฐ’์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

๊ณผ๊ฑฐ Java 7 ์ด์ „์—๋Š” Perm ์˜์—ญ์—์„œ Constant Pool์ด ๊ด€๋ฆฌ๋˜์—ˆ๋Š”๋ฐ, ๋ฉ”๋ชจ๋ฆฌ ํ™•์žฅ ๋ถˆ๊ฐ€๋กœ ์ธํ•œ OOM ๋ฐœ์ƒ์œผ๋กœ Java 7 ์ดํ›„๋กœ Heap Memory์—์„œ Constant Pool์„ ๊ด€๋ฆฌํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค. ์ฆ‰, ๋ฌธ์ž์—ด์ด GC์˜ ์˜์—ญ์— ํฌํ•จ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

'Programming > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

F006 - Throwable, Exception, Error  (0) 2021.05.11
F005 - Stack & Heap Memory  (0) 2021.05.08
F003 - Access Modifier  (0) 2021.05.06
F002 - Primitive Type  (0) 2021.05.06
F001 - psvm  (0) 2021.05.06