Wrapper Classes and Autoboxing
Java has eight primitive data types (int, double, etc.). Sometimes you need to treat them as objects – for example, when using collections like ArrayList. Wrapper classes provide this capability.
Primitive to Wrapper Mapping
- int → Integer
- double → Double
- char → Character
- boolean → Boolean
- byte → Byte
- short → Short
- long → Long
- float → Float
Autoboxing and Unboxing
Java automatically converts between primitives and their wrapper classes.
// Autoboxing: primitive to wrapper
Integer num = 42; // int to Integer automatically
// Unboxing: wrapper to primitive
int value = num; // Integer to int automatically
Why Important for DSA?
Collections (ArrayList, HashMap, etc.) work with objects, not primitives. Wrapper classes allow you to store primitive values in collections.
ArrayList list = new ArrayList<>();
list.add(5); // autoboxing int to Integer
int first = list.get(0); // unboxing Integer to int
Useful Wrapper Methods
Integer.parseInt(String)– convert string to intInteger.toString(int)– convert int to stringCharacter.isDigit(char)– check if char is digitDouble.compare(double, double)– compare doubles
Two Minute Drill
- Wrapper classes let primitives act as objects.
- Autoboxing: primitive → wrapper automatically.
- Unboxing: wrapper → primitive automatically.
- Essential for using collections with primitive values.
- Common methods: parseInt(), toString(), isDigit(), etc.
Need more clarification?
Drop us an email at career@quipoinfotech.com
