
Integer 초기화 방식에 대해서 알아보자
Integer i = 123;해당한 줄 이 어떻게 생성이 될까?
@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}아래의 valueOf 메서드로 넘어가게 된다.
Integer는 -127 ~ 128까지의 범위의 수일 때 캐시를 저장하게 된다.
128 이상의 값을 저장하기 위해서선 VM option을 사용하면 된다.
 -XX:AutoBoxCacheMax=size.이러한 캐싱된 인스턴스는 autoboxing을 통해 새로운 인스턴스를 만들지 않고 사용된다.
예시
Integer integer1 = 3;
Integer integer2 = 3;
if (integer1 == integer2)
	System.out.println("integer1 == integer2");
else
	System.out.println("integer1 != integer2");
Integer integer3 = 300;
Integer integer4 = 300;
if (integer3 == integer4)
	System.out.println("integer3 == integer4");
else
	System.out.println("integer3 != integer4");
3의 경우에는 캐시의 범위에 있기 때문에 같은 인스턴스를 바라보지만
300은 127보다 크기 때문에 각각 다른 인스턴스를 생성한다.
캐시의 내부
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer[] cache;
        static Integer[] archivedCache;
        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    h = Math.max(parseInt(integerCacheHighPropValue), 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;
            // Load IntegerCache.archivedCache from archive, if possible
            VM.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;
            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
        private IntegerCache() {}
    }정리
캐싱이 안 되는 -128 ~ 127 범위 밖
- new 키워드를 사용하면 주소가 달라진다.
- 나머지도 다 주소 값이 다르다.
- 전부 내부적으로 new Integer()를 호출하고 있다. (프리머티브 타입은 컴파일러가 자동으로 오토 박싱 하면서 생성해 준다.)
 
- 위에 a1 == a2 // true 근데 b1 == b2 // false
- 컴파일러가 자동으로 new Integer() 를 생성해 주는 건 같은데 a는 캐싱이 돼서 주소 값이 같고 b는 캐싱이 안돼서 주소 값이 다르다.
 
References
https://javapapers.com/java/java-integer-cache/
Java Integer Cache - Javapapers
This Java article is to introduce and discuss about Integer Cache. This is a feature introduced in Java 5 to save memory and improve the per
javapapers.com
https://www.geeksforgeeks.org/comparison-autoboxed-integer-objects-java/
Comparison of Autoboxed Integer objects in Java - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'Java > Java' 카테고리의 다른 글
| (JAVA) equals에 대해서(2) (0) | 2020.01.31 | 
|---|---|
| (JAVA) equals에 대해서(1) (0) | 2020.01.31 | 
| (JAVA) wait() 와 notify() (0) | 2020.01.26 | 
| (JAVA) 쓰레드 동기화 (0) | 2020.01.25 | 
| (JAVA) 쓰레드의 실행제어 - join() (0) | 2020.01.25 | 
 
										
									 
										
									 
										
									