[Java] wrapper ν΄λμ€μ μ μ, λ©λͺ¨λ¦¬, μΊμ±
μλ‘
μλ°λ₯Ό 곡λΆνλ€ λ³΄λ©΄ int, Integer κ·Έλ¦¬κ³ double, Doubleμ κ°μ΄ 첫 μ΄λ¦μ΄ λλ¬Έμλ‘ μ μΈλ κ²μ λ³Ό μ μμ΅λλ€. μ΄λ² κ²μκΈμμλ μ΄λ¬ν κ²λ€μ΄ 무μμΈμ§μ λν΄μ μ λ¦¬ν΄ λ³΄κ² μ΅λλ€.
λνΌ(Wrapper) ν΄λμ€
κ°μ²΄μ§ν₯ κ°λ μμ λͺ¨λ κ²μ κ°μ²΄λ‘ λ€λ£¨μ΄μ ΈμΌ ν©λλ€. κ·Έλ¬λ μλ°μμλ 8κ°μ κΈ°λ³Έν(privitive type)μ κ°μ²΄λ‘ λ€λ£¨μ§ μκ³ μλλ° λμ μ±λ₯μ μ»κΈ° μν΄μ κΈ°λ³ΈνμΌλ‘ λ€λ£¨κ³ μμμ΅λλ€. νμ§λ§ 맀κ°λ³μλ‘ κ°μ²΄λ₯Ό μꡬνκ±°λ, κΈ°λ³Ένμ΄ μλ κ°μ²΄λ‘ κ°μ μ μ₯ν΄μΌ ν λ, κ°μ²΄ κ°μ λΉκ΅κ° νμν λ λ±λ±μ κ²½μ°μμ κΈ°λ³Έν κ°λ€μ κ°μ²΄λ‘ λ°ννμ¬ μμ μ μνν λκ° μμ΅λλ€.
μ΄λ μ¬μ©λλ κ²μ΄ λνΌ(wrapper) ν΄λμ€μ λλ€. 8κ°μ κΈ°λ³Ένμ λννλ 8κ°μ wrapper ν΄λμ€κ° μλλ°, μ΄ ν΄λμ€λ€μ μ΄μ©νλ©΄ κΈ°λ³Έν κ°μ κ°μ²΄λ‘ λ€λ£° μ μμ΅λλ€.
κΈ°λ³Έν | λνΌν΄λμ€ | μμ±μ | μ |
boolean | Boolean | Boolean (boolean value) Boolean (String s) |
Boolean b = new Boolean(true); Boolean b2 = new Boolean("true"); |
char | Character | Character (char value) | Character c = new Character('a'); |
byte | Byte | Byte (byte value) Byte (String s) |
Byte b = new Byte(10); Byte b2 = new Byte("10"); |
short | Short | Short (short value) Short (short s) |
Short s = new Short(10); Short s2 = new Short("10"); |
int | Integer | Integer (int value) Integer (String s) |
Integer i = new Integer(100); Integer 2 = new Integer("100"); |
long | Long | Long (long value) Long (String s) |
Long l = new Long(100); Long l = new Long("100"); |
float | Float | Float (double value) Float (float value) Float (String s) |
Float f = new Float(1.0); Float f2 = new Float(1.0f); Float f3 = new Float("1.0f"); |
double | Double | Double (double value) Double (String s) |
Double d = new Double(1.0); Double d = new Double("1.0"); |
wrapper ν΄λμ€λ€μ λͺ¨λ equals()κ° μ€λ²λΌμ΄λ© λμ΄μκΈ° λλ¬Έμ μ£Όμ κ°μ΄ μλ κ°μ²΄κ° κ°μ§κ³ μλ κ°μ λΉκ΅νκ² λ©λλ€. λν wrapper ν΄λμ€λ€μ MAX_VALUE, MIN_VALUE, SIZE, TYPE λ±μ static λ©€λ²λ₯Ό 곡ν΅μ μΌλ‘ κ°μ§κ³ μμ΅λλ€.
Wrapper ν΄λμ€μ λ©λͺ¨λ¦¬ ν λΉ
Wrapper ν΄λμ€λ€μ μ°λ¦¬κ° κΈ°μ‘΄μ μλ κΈ°λ³Ένκ³Όλ λ€λ₯Έ ν¬κΈ°μ λ©λͺ¨λ¦¬λ₯Ό ν λΉν©λλ€.
int a = 4;
Integer b = 16;
μ μ½λλ₯Ό μμλ‘ λ€μ΄λ³΄λ©΄ aλ intμ ν¬κΈ°μΈ 4byteκ° ν λΉλμ§λ§, b κ°μ κ²½μ° 16byteκ° ν λΉλ©λλ€. κ·Έ μ΄μ λ wrapperλ ν΄λμ€ ν¬μΈν°, νλκ·Έ, λ½(Lock), int κ° κΉμ§ μ΄ 4κ°μ valueλ₯Ό μ μ₯ν΄μΌ νκΈ° λλ¬Έμ λλ€.
Wrapper ν΄λμ€μ μΊμ±
Wrapper ν΄λμ€λ μΌμ ꡬκ°μ κΈ°λ³Έ κ°μ 미리 μμ±ν΄ λκ³ μμ΅λλ€. μλ μ½λλ μ€μ Integer ν΄λμ€μ μ½λμ λλ€.
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* jdk.internal.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
νμ¬ -128 ~ 127κΉμ§μ κ°μ κ°μ§λ Integer μΈμ€ν΄μ€λ₯Ό 미리 μμ±νκ³ μλ κ²μ μ μ μμ΅λλ€.
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
@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 λ©μλλ₯Ό μ¬μ©νμ λ -127 ~ 128 λ²μμ κ°μ΄ λ€μ΄μ¨λ€λ©΄ μΊμ±λ κ°μ λ΄λ³΄λ΄κ³ λ²μ λ°μ΄λΌλ©΄ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό ν λΉν©λλ€.
Integer ν΄λμ€ λΏλ§ μλλΌ, Byte, Short, Long, Character μμ μ체μ μΌλ‘ μΊμ±μ μ¬μ©νκ³ μμ§λ§ Floatμ Doubleλ μ€μμ΄κΈ° λλ¬Έμ λ²μ μμΈ‘μ΄ νλ€μ΄ μ체μ μΌλ‘ μΊμ±μ νμ§ μμ΅λλ€.