Wrapper Classes in Java

  • Basically, we work with numbers and we use primitive data types such as byte, int, long, double, etc. There are certain situations to use objects in place of primitives and the Java platform provides wrapper classes for each of the primitive data types. For example, As we know Collections does not support primitive types but Collections does support Wrapper classes.
  • These classes “wrap” the primitive data type value in an object. Hence the name “Wrapper Classes”. All wrapper classes are immutable classes.
  • Wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. Since J2SE 5.0, autoboxing and unboxing feature convert primitive into object and object into primitive automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.

8 Wrapper Classes in java

The eight classes of a java.lang package are known as wrapper classes in java
Primitive TypeWrapper class
 booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

When to use Wrapper classes?

  • When you are working collections to store numeric values. Because collection objects can’t store primitive values.
  • To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  • To convert a String representation of primitive value into a primitive data type.

Wrapper Classes Hierarchy


Wrapper Classes API Guide

Let's learn each wrapper class APIs with examples. Click on below link to learn corresponding wrapper class methods with examples.

Comments

Post a Comment

Leave Comment