Wrapper Classes in Java
Introduction
Java defines a wrapper class for each of its primitive data types. A Wrapper class in Java is one whose object wraps or contains primitive data types. Wrapper classes help you write cleaner code, which is easy to read.
Need & Advantages of Wrapper Class
- Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, HashSet, etc.) deal with objects only.
- Serialization can be achieved via Wrapper classes. Serialization is converting the objects into streams. If we have a primitive value, we can convert it into objects through the wrapper classes.
- If we pass a primitive value as a method parameter, it will not change the original value. But, if we convert the primitive value into an object, we can change its original value.
- The java.util package provides the utility classes to deal with objects.
- Synchronization can be achieved via Wrapper classes as synchronization works with objects in Multithreading.
Wrapper Classes and Their Hierarchy
Primitive data types and their corresponding wrapper class
Hierarchy of wrapper classes
- All the numeric wrapper classes (Byte, Short, Integer, Long, Float, Double) extend the class java.lang.Number.
- Classes Boolean and Character directly extend the class Object.
- All the wrapper classes implement the interfaces java.io.Serializable and java.lang.Comparable.
Autoboxing and Unboxing
Autoboxing
Autoboxing is the automatic conversion of a primitive data type to an object of the corresponding wrapper class.
For example, boolean to Boolean, char to Character, byte to Byte, short to Short, int to Integer, long to Long, float to Float, and double to Double.
class Autoboxing {
public static void main(String[] args) {
Boolean bool = true;
Character ch = 'c';
Byte b = 2;
Short s = 2;
Integer i = 1;
Long l = 4L;
Float f = 1.2f;
Double d = 1.2;
}
}
Unboxing
Unboxing is just the reverse process of autoboxing. It is the automatic conversion of a wrapper class object to the corresponding primitive data type.
Ways to create an object of Wrapper Class
You can create objects of all the wrapper classes in multiple ways
- Autoboxing
- Constructor
All the wrapper classes define constructors to create an object using a corresponding primitive value.
All wrapper classes (except Character) also define a constructor that accepts a String argument representing the primitive value that needs to be wrapped. - Static Method
All wrapper classes (except Character) also define a static method that accepts a String argument representing the primitive value that needs to be wrapped.
class WrapperUsingConstructor {
public static void main(String[] args) {
// Constructors that accepts primitive Value
Boolean bool1 = new Boolean(true);
Character char1 = new Character('c');
Byte byte1 = new Byte((byte) 3);
Short short1 = new Short((short) 2);
Integer int1 = new Integer(1);
Long long1 = new Long(4L);
Float float1 = new Float(1.2f);
Double double1 = new Double(1.5);
// Constructors that accepts String Value
Boolean bool2 = new Boolean("true");
// Character char2 = new Character("c"); -> Won't compile
Byte byte2 = new Byte("3");
Short short2 = new Short("2");
Integer int2 = new Integer("1");
Long long2 = new Long("4");
Float float2 = new Float("1.2");
Double double2 = new Double("1.5");
}
}
class WrapperUsingStaticMethod {
public static void main(String[] args) {
// Static methods that accepts primitive Value
Boolean bool1 = Boolean.valueOf(true);
Character char1 = Character.valueOf('c');
Byte byte1 = Byte.valueOf((byte) 3);
Short short1 = Short.valueOf((short) 2);
Integer int1 = Integer.valueOf(1);
Long long1 = Long.valueOf(4L);
Float float1 = Float.valueOf(1.2f);
Double double1 = Double.valueOf(1.5);
// Static methods that accepts String Value
Boolean bool2 = Boolean.valueOf("true");
// Character char2 = Character.valueOf("c"); -> Won't compile
Byte byte2 = Byte.valueOf("3");
Short short2 = Short.valueOf("2");
Integer int2 = Integer.valueOf("1");
Long long2 = Long.valueOf("4");
Float float2 = Float.valueOf("1.2");
Double double2 = Double.valueOf("3.5");
}
}
Retrieving primitive value from the wrapper object
To get a primitive data type value corresponding to a wrapper object, you can use the static utility method dataTypeValue. Each wrapper class defines a method to retrieve the primitive value.
class WrapperToPrimitive {
public static void main(String[] args) {
Boolean bool2 = Boolean.valueOf("true");
Character char2 = Character.valueOf('c');
Byte byte2 = Byte.valueOf("3");
Short short2 = Short.valueOf("2");
Integer int2 = Integer.valueOf("1");
Long long2 = Long.valueOf("4");
Float float2 = Float.valueOf("1.2");
Double double2 = Double.valueOf("3.5");
// Retrieve primitive value from the wrapper object
boolean bool = bool2.booleanValue();
char ch = char2.charValue();
byte b = byte2.byteValue();
short s = short2.shortValue();
int i = int2.intValue();
long l = long2.longValue();
float f = float2.floatValue();
double d = double2.doubleValue();
}
}
Parsing a string value to a primitive type
To get a primitive data type value corresponding to a string value, you can use the static utility method parseDataType. Each wrapper class (except Character) defines a method to parse a String to the corresponding primitive value.
class StringToPrimitive {
public static void main(String[] args) {
// Retrieve primitive value from string
boolean bool = Boolean.parseBoolean("true");
byte b = Byte.parseByte("2");
short s = Short.parseShort("3");
int i = Integer.parseInt("4");
long l = Long.parseLong("5");
float f = Float.parseFloat("6.6");
double d = Double.parseDouble("7.7");
}
}
Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer
For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97
Reach me out at:
https://www.linkedin.com/in/gauravshah97/