Wrapper Class

Boxing๊ณผ Unboxing, AutoBoxing

Intro

Java์—๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ Wrapper class๊ฐ€ ์กด์žฌํ•œ๋‹ค

  • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(Primitive data type): int, long, float, double, boolean ๋“ฑ

  • Wrapper class: Integer, Long, Float, Double, Boolean ๋“ฑ

Boxing & Unboxing

  • Boxing

    • Primitive data type โ†’ Wrapper class๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ

      int n = 100;
      Integer num = new Integer(n);
  • Unboxing

    • Wrapper class โ†’ Primitive data type์œผ๋กœ ๋ณ€ํ™˜

      Integer num = new Integer(100);
      int n = num.intValue();

Auto Boxing

  • JDK 1.5๋ถ€ํ„ฐ๋Š” Java ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ฐ•์‹ฑ๊ณผ ์–ธ๋ฐ•์‹ฑ์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์— ์ž๋™์œผ๋กœ ๋ณ€ํ™˜์„ ํ•ด์ค€๋‹ค โ†’ Auto Boxing & Auto Unboxing

    • ์ด ๊ธฐ๋Šฅ์€ ๊ฐ Wrapper class์— ์ƒ์‘ํ•˜๋Š” Primitive data type ์ผ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค

      // ์˜คํ†  ๋ฐ•์‹ฑ
      int n = 100;
      Integer num = n;
      
      // ์˜คํ†  ์–ธ๋ฐ•์‹ฑ
      Integer num = new Integer(100);
      int n = num;
  • ์ฃผ์˜ํ•  ์ : ์„ฑ๋Šฅ

    • ํŽธ์˜์„ฑ์„ ์œ„ํ•ด Auto Boxing๊ณผ Auto Unboxing์ด ์ œ๊ณต๋˜๊ณ  ์žˆ์ง€๋งŒ, ๋‚ด๋ถ€์ ์œผ๋กœ ์ถ”๊ฐ€ ์—ฐ์‚ฐ ์ž‘์—…์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Auto Boxing & Auto Unboxing ์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ๋™์ผ ํƒ€์ž… ์—ฐ์‚ฐ ์ด ์ด๋ฃจ์–ด์ง€๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค

Last updated