Wrapper Class in Java

Wrapper Class in Java - Types, Autoboxing & Unboxing Explained (2026)

Wrapper Class in Java

Wrapper Class in Java

What is Wrapper Class?

  1. As we know, Java is not a pure Object Oriented Language because it uses primitive data types. So Java provides Wrapper classes to convert primitive types to pure Object types.
  2. Wrapper classes are predefined classes to wrap primitive data types as Object types.
  3. Wrapper class can hold only its respective primitive data type value.
  4. Wrapper classes are used in Collection Framework, Hibernate Framework and Spring Framework etc., because collections work only with objects.
  5. There are 8 Wrapper Classes for 8 primitive types:

Types of Wrapper Classes

Primitive Type Wrapper Type
byteByte
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean

What is Autoboxing and Unboxing?

In Java 5, a new feature was introduced called Autoboxing and Unboxing.

Autoboxing

When you wrap primitive data type to its respective Wrapper class, that process is called Autoboxing. Conversion is automatic.

Example:

int i = 10;        // primitive
Integer j = i;     // Autoboxing - primitive to object automatically

// Internally: Integer j = Integer.valueOf(i);

Unboxing

When you convert Wrapper object to its respective primitive data type, that process is called Unboxing.

Example:

Integer i = 10;  // Wrapper object
int j = i;         // Unboxing - object to primitive automatically

// Internally: int j = i.intValue();

Conclusion

Wrapper classes make Java more object-oriented. They are essential for Collections (ArrayList, HashMap), Frameworks (Hibernate, Spring), and provide useful utility methods like Integer.parseInt(), valueOf() etc. Use autoboxing/unboxing to simplify code since Java 5.

Next: Parsing, Type Conversion, Wrapper Class Methods

2 comments:

Vinit Pal said...

Thanks for all your code and new technology

RohitMachiwal said...

thank you so much for this sir