๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

[Java] wrapper ํด๋ž˜์Šค์˜ ์ •์˜, ๋ฉ”๋ชจ๋ฆฌ, ์บ์‹ฑ

by dkswnkk 2022. 4. 8.

์„œ๋ก 

์ž๋ฐ”๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋ฉด 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๋Š” ์‹ค์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ”์œ„ ์˜ˆ์ธก์ด ํž˜๋“ค์–ด ์ž์ฒด์ ์œผ๋กœ ์บ์‹ฑ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

[Java] (Project, Package, Class, Method) Naming ๊ทœ์น™  (0) 2022.05.16
[Java] String format() method  (0) 2022.05.14
[Java] Object to int  (0) 2022.05.06
[Java] StringBuffer์™€ StringBuilder  (0) 2022.04.06
[Java] ArrayList vs LinkedList  (0) 2022.04.06
[Java] String ํด๋ž˜์Šค  (0) 2022.04.05

๋Œ“๊ธ€