Wrapper Classes | Autoboxing | Unboxing | Java

πŸ” What is Autoboxing?

Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper class.

βœ… Example:

int num = 10;
Integer obj = num; // autoboxing

Java automatically converts int β†’ Integer.


πŸ” What is Unboxing?

Unboxing is the reverse process – converting a wrapper class object back to a primitive type.

βœ… Example:

Integer obj = 20;
int num = obj; // unboxing

Java converts Integer β†’ int.


πŸ“¦ Wrapper Classes in Java

Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

🧠 Why Use Wrapper Classes?

  • Used in Collections (like ArrayList, HashMap)
  • Provide utility methods (e.g., Integer.parseInt())
  • Allow null values
  • Enable autoboxing/unboxing

πŸ” Real Example:

ArrayList<Integer> list = new ArrayList<>();
list.add(5); // primitive int is autoboxed to Integer

int val = list.get(0); // Integer is unboxed to int

Β 


βœ… Summary:

Concept Description Example
Autoboxing int β†’ Integer Integer a = 10;
Unboxing Integer β†’ int int b = a;
Wrapper Object version of primitive Integer, Double, Boolean

μžλ°”] java κΈ°λ³Έ νƒ€μž…μ™€ wapper(래퍼) 클래슀, λ°•μ‹±κ³Ό μ–Έλ°•μ‹±

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.