Instance Cache
์๋ฐ์ wrappingํ์
์ด ๊ฐ์ ์ด๊ธฐํ ํ๋ ๋ฐฉ์์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
Integer.valueof()
๋ก ์ค๋ช
ํฉ๋๋ค
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
...
...
์ง์ Cache ์ฌ์ด์ฆ๋ฅผ ์ง์ ํด ๋์ง ์๋๋ค๋ฉด -127~128(1byte)
๋งํผ์ ์บ์ํด๋ก๋๋ค.
public static void main(String[] args) throws IOException, InterruptedException {
Integer add1 = Integer.valueOf(1);
Integer add2 = Integer.valueOf(1);
System.out.println("same ? :" + (add2 == add1));
//same ? :true
Integer add3 = Integer.valueOf(258);
Integer add4 = Integer.valueOf(258);
System.out.println("same ? :" + (add3 == add4));
//same ? :false
}
์บ์ ๋ฒ์์ ์๋ add1, add2์ ๊ฐ์ ์ธ์คํด์ค ์บ์ ๋ฒ์ ๋ฐ๊นฅ์ ์๋ add3, add4๋ ๋ค๋ฅธ ์ธ์คํด์ค์ธ๊ฑธ ํ์ธ ํ ์ ์์ต๋๋ค.
Integer cache ์ด์ธ์๋ ByteCache, ShortCache, LongCache, CharcaterCache๊ฐ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์กด์ฌํฉ๋๋ค.
Last updated