Wrapper class in Java is a class whose object wraps a primitive type. When you need an object instead of a primitive type you can use Wrapper class to wrap the primitive type value in an object and use that object.
When is Java Wrapper class needed
Some of the scenarios where you may need a Wrapper class are listed below.
- If you have any method that expects an object as argument.
- If you want to store primitive types in any data structure which can save only objects for example if you have an array of Objects (Object arr) or any of the Java Collections class that can store only objects (List<Integer> numberList).
- Wrapper classes also have many other utility methods and fields that you can use if you wrap primitive type into a Wrapper class.
For example you can use constant fields MIN_VALUE and MAX_VALUE of the Wrapper class to get the upper and lower bounds of the data type.
You can also use methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems.
Wrapper classes in Java for primitive types
For all eight primitive data types there is a corresponding Wrapper class in Java.
|Primitive Type||Wrapper class|
Out of these 8 wrapper classes 6 are numeric. All of the numeric wrapper classes are subclasses of the abstract class Number.
Conversion using methods of the Wrapper classes
Though Autoboxing and unboxing (Java 5 onward) can automatically convert primitive into object and object into primitive but you may still use methods of the wrapper classes if you want to convert values to and from other primitive types.
Java Wrapper class examples
1- Converting int to String
int i = 22; // Converting to String String str = Integer.toString(i);
2- Converting double to int in Java
Double d = 147.89d; int val = d.intValue(); System.out.println("Converted int value- " + val);
3- Converting float to double in Java
Float f = 147.89f; double val = f.doubleValue(); System.out.println("Converted double value- " + val);
4- Wrapping char into a Character Wrapper class. Note that such constructors for all the Wrapper classes are deprecated after Java 8.
Character c = new Character('a');
Autoboxing and unboxing
Automatic conversion of primitive type to its corresponding Wrapper class is known as Autoboxing. You don’t need to do that conversion automatically.
For example, directly assigning int value to an Integer object. Conversion happens automatically.
Integer i = 10; // primitive int value 10 converted to Integer
Adding int value to an ArrayList that stores Integer object. Autoboxing automatically wraps int value to an Integer object and then it is stored into the List.
List<Integer> numList = new ArrayList<Integer>(); numList.add(10);
Same way Wrapper class object can be converted to corresponding primitive type automatically through Unboxing.
For example- When an Integer stored in a List is retrieved you can assign it to an Integer or to an int too. In that case Conversion from Integer (Wrapper class) to int is done automatically.
List<Integer> numList = new ArrayList<Integer>(); numList.add(10); Integer num = numList.get(0); System.out.println("num- " + num); // value as int - unboxing int i = numList.get(0); System.out.println("num- " + i);
- Equality And Relational Operators in Java
- Methods in Java
- Package in Java
- Abstract Class in Java
- Java break Statement With Examples
- Type Casting And Type Conversion in Java
- Shallow Copy Vs Deep Copy in Java Object Cloning
- Access Modifiers in Java
That’s all for the topic Wrapper Class in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like