Understanding The Concept Of Wrapper Classes in Java
Java encompasses many fascinating concepts. As we explore one topic, we often discover related ideas. One such idea is the concept of the Wrapper Class, which is very intriguing. Let's take a deeper look at the Wrapper Class.
In the Java programming language, every variable and expression has a type. The type can be a primitive type or a reference type. Let's look at how the type system works in Java.
Primitive Type
The primitive types are
boolean
byte
short
char
int
float
long
double
Primitive types represent a single value. The variable values are stored directly in the stack memory. This makes them memory-efficient and faster to access.
The table below shows the memory footprint of each primitive type.
Primitive Type | Bits |
boolean | 1 |
byte | 8 |
short | 16 |
char | 16 |
int | 32 |
float | 32 |
long | 64 |
double | 64 |
Reference Type
The reference types are
Class
Interface
Array
Reference types hold values and behaviors. Static/instance methods provide the behaviors, and static/instance variables provide values. Java provides built-in reference types like Object, String, Array, etc. Reference types can also be user-defined using the class, interface, and array constructs.
Reference types store references to memory locations on the heap and are slower to access. E.g., consider a user-defined class named Student that has 3 member variables:
int age
String firstName
String lastName
The image below shows references to 2 objects of the Student class: student1 and student2. Each object in the heap further holds references to String objects for firstName and lastName.

While reference types offer more functionality and flexibility, they come with a higher memory overhead.
Wrapper class
Java provides a Collection framework to represent groups of Objects. The Collection framework contains interfaces and general-purpose implementations that are reusable. They can handle ordered/unordered with/without duplicates, etc.
The core Collection interfaces are listed below:
Collection
Set
SortedSet
List
Queue
Deque
Map
SortedMap
The Collection framework works with reference types only. To allow primitive types to be used with the Collection framework, Java provides Wrapper classes for each primitive type. A wrapper class can wrap another class and help provide an abstraction concept between the original class and the client. The Wrapper class encompasses all eight primitive data types, enabling the instantiation of objects in either single or multiple Classes. This is known as the Primitive Wrapper Class. Each primitive datatype has a corresponding wrapper class.
E.g. this is not valid
List<int> numbers = new ArrayList<int>();But, this is valid:
List<Integer> numbers = new ArrayList<Integer>();The image below shows the class hierarchy of the Wrapper classes provided by Java.

The Wrapper classes can be initialized in the same way as their respective primitive type. E.g.
Integer age = 5;
Double speed = 5.99d;Why do we need a Wrapper Class?
Many Libraries, Stream API, and Java Collections like List, Set, and Map work only with objects and not primitive data
Sometimes we need a primitive value to be treated as an object, to use in methods which are specific to wrapper classes.
Wrapper Classes have many useful methods, like converting a string to an integer.
String num=”10”;
int result=Integer.parseInt(num);
Advantages and Disadvantages of Reference Datatype:
The advantages of reference data types are that they contain many useful methods.For example, the String class includes length(), concat(), toUpperCase(), toLowerCase(), trim(), and toString().
Reference data types are used in the ArrayList Collection.
The disadvantage is that accessing the value is slower. For example, if we need to fetch the value of a primitive data type that is inside a Wrapper Class, it takes longer.
Primitive data types are much easier and faster to access their values.
Example:
A practical application of the Wrapper class is that when databases change in the long term(e.g., from MySQL to MongoDB), it is easier to modify the implementation of the Wrapper class than to adjust each instance where it is invoked.
Boxing
When we assign a few primitive data types and use the corresponding wrapper class to create a reference datatype, this is called Boxing.
That is taking a primitive value and storing it in an object
For Eg.
int num = 87;
Integer num1=new Integer(num);//boxingHere, num is a primitive variable, and num1 is a reference variable. We are assigning a reference variable to a primitive data type.

Autoboxing
When a primitive data type is automatically converted to its corresponding wrapper class, it is called Autoboxing. When we directly assign values to each of the reference datatypes, for Eg.
Boolean a=true;
Character splch=’@’;
Double pi=3.14;
Integer const=123;
String brother =” Bro”;Earlier, we discussed an example for Boxing. Similarly, for Autoboxing:
int num = 87;
Integer num1=(num);//AutoboxingHere, num was originally a primitive datatype. But when you assign num to num1, num automatically converts to a reference type.
Unboxing
Unboxing is the reverse of Autoboxing. It occurs when a wrapper class is converted to a primitive value or when a reference data type is treated as a primitive value. For Eg.
int num = 87;
Integer num1=(num);//autoboxing
Int num2=num1.intValue();//unboxing
System.out.println(num2);Output:
87
Here we are obtaining a value of object type from a primitive type.
If we have a string that contains a number, how do we perform arithmetic operations on it?
Let us see, for example
String str =”32”;
int number=Integer.parseInt(str);
System.out.println(number+25);Output:
57
Here, Integer is a class that performs various functions.Integer.parseInt converts a value to a number so it can perform arithmetic operations.
Advantages of Autoboxing and Unboxing
No need for explicit type casting and conversions.Easier readability of code.
It facilitates the use of primitive types in collections that require objects, such as ArrayList.
Used to fetch values from wrapper objects.
This is also used to pass primitive values to functions or methods that expect objects.
Atomic Wrapper Class:
Atomic wrapper classes in Java are utilized in multithreaded environments. They provide thread-safe modifications for primitive values like boolean, long, and integer. These are the most commonly used atomic variables. The corresponding variables are AtomicBoolean, AtomicLong, and AtomicInteger. Additionally, there is AtomicReference.These objects are mutable, meaning modifications can be made after their creation. However, operations like incrementing and setting values are done atomically, ensuring consistency in a multi-threaded environment.
Here are some examples of atomic operations:
getAndIncrement()
compareAndSet()
In essence, Wrapper classes are used for the conversion of primitive types to object types, while Atomic operations are used when there is concurrency modifications.
Hope you understood better about Wrapper Class.Thank you for reading!!!


