Member-only story
Interview Question why we need “WRAPPER” Class in java | Autoboxing & Unboxing
In Java, there are eight primitive data types:
byte
,short
,int
,long
,float
,double
,char
, andboolean
. Each primitive type has a corresponding wrapper class that provides additional functionality and enables their use in various Java APIs
Why Wrapper Classes?
1. Compatibility with Collections Framework
The Java Collections Framework (e.g., ArrayList
, HashMap
) requires objects (reference types) rather than primitive types. Wrapper classes allow primitive values to be used as objects in these collections.
2. Conversion Between Types
Wrapper classes provide methods for converting between different data types and performing various operations. For instance, you can convert an int
to a float
or a double
to a String
.
Use Cases of Wrapper Classes
1. Converting int
to float
public class Custom {
public static void main(String[] args) {
int x = 10;
Integer i = new…