What is a wrapper class in java? Explain with examples.

What is a wrapper class in java? Explain with examples.

In Java, a wrapper class is defined as a class in which a primitive value is wrapped up. These primitive wrapper classes are used to represent primitive data type values as objects. The Java platform provides wrapper classes for each of the primitive data types. For example, Integer wrapper class holds primitive ‘int’ data type value. Similarly, Float wrapper class contain ‘float’ primitive values, Character wrapper class holds a ‘char’ type value, and Boolean wrapper class represents ‘boolean’ value.

Examples of Java Wrapper Classes:To describe the application of wrapper classes in java, let us create an Integer wrapper class object with primitive int type value and get back the primitive value from the wrapper object.

int i = 26; // Primitive data type 'int'

// Integer Wrapper class instantiation

Integer i_Obj = new Integer(i);

// Unwrapping primitive data 'int' from wrapper object

int i2 = i_Obj.intValue();

List of all Wrapper Classes in java:

Primitive data type                   Wrapper class
byte                                             Byte
short                                            Short
int                                                Integer
long                                             Long
float                                             Float
double                                         Double
char                                            Character
boolean                                       Boolean

Read More →