Member-only story

Interview Question why we need “WRAPPER” Class in java | Autoboxing & Unboxing

Sanjay Singh
2 min readSep 5, 2024

--

In Java, there are eight primitive data types: byte, short, int, long, float, double, char, and boolean. Each primitive type has a corresponding wrapper class that provides additional functionality and enables their use in various Java APIs

Interview Question why we need Wrapper Class in java

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…

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

Responses (2)