In this lesson, you will learn
Wrapper classes in Java are a set of classes that provide a way to use primitive data types (such as int, char, boolean, etc.) as objects.
Since Java is an object-oriented language, there are situations where you need to work with objects rather than primitives, such as when using collections (e.g., ArrayList, HashMap), which only work with objects.
Wrapper classes allow you to convert primitives into objects and vice versa.
Each primitive data type in Java has a corresponding wrapper class:
| Primitive Data Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Object Representation of Primitives:
Wrapper classes allow primitive data types to be used as objects. For example, you can store an int in an Integer object.
Utility Methods:
Wrapper classes provide utility methods for converting between primitives and strings, parsing strings, and performing other operations. For example:
Integer.parseInt(String s) converts a string to an int.
Integer.toString(int i) converts an int to a string.
Autoboxing and Unboxing:
Autoboxing: Automatic conversion of a primitive to its corresponding wrapper class object. For example:
Integer num = 10; // Autoboxing: int to Integer
Unboxing: Automatic conversion of a wrapper class object to its corresponding primitive. For example:
int num = new Integer(10); // Unboxing: Integer to int
Immutability:
Wrapper classes are immutable, meaning their values cannot be changed once they are created. If you need a different value, you must create a new object.
Use in Collections:
Collections like ArrayList, HashMap, etc., can only store objects. Wrapper classes allow primitives to be stored in these collections. For example:
ArrayList<Integer> list = new ArrayList<>(); list.add(5); // Autoboxing: int to Integer
public class WrapperExample {
public static void main(String[] args) {
// Autoboxing: primitive to wrapper
Integer num1 = 10; // int to Integer
Double num2 = 5.5; // double to Double
// Unboxing: wrapper to primitive
int num3 = num1; // Integer to int
double num4 = num2; // Double to double
// Utility methods
String str = "123";
int num5 = Integer.parseInt(str); // String to int
String str2 = Integer.toString(num5); // int to String
// Using in collections
ArrayList list = new ArrayList<>();
list.add(10); // Autoboxing
list.add(20);
System.out.println("List: " + list);
System.out.println("Parsed int: " + num5);
}
}
Compatibility with Object-Oriented Features:
Wrapper classes allow primitives to be used in contexts that require objects, such as collections, generics, and reflection.
Utility Methods:
They provide useful methods for converting, parsing, and manipulating data.
Nullability:
Wrapper classes can hold null values, whereas primitives cannot.
Performance Overhead:
Wrapper classes introduce additional overhead due to object creation and garbage collection, which can impact performance in scenarios requiring high efficiency.
Memory Usage:
Wrapper objects consume more memory than their primitive counterparts.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.