In Java, primitive data types (like int
, char
, boolean
, etc.) do not behave like objects. However, sometimes it's necessary to treat primitives as objects to take advantage of object-oriented features. Without wrapper classes, we wouldn't be able to use primitives in contexts where objects are required, such as collections.
What is it?
Wrapper classes in Java are object representations of primitive data types. They "wrap" primitive values in an object, providing methods and functionality that primitives don't have. Each primitive type has a corresponding wrapper class, enabling the use of primitives in situations where objects are required (e.g., in collections like ArrayList
).
Primitive Types and Their Corresponding Wrapper Classes:
\| Primitive Type \| Wrapper Class \|
\|--------------------\|--------------------\|
\| int
\| Integer
\|
\| double
\| Double
\|
\| boolean
\| Boolean
\|
\| char
\| Character
\|
\| long
\| Long
\|
\| float
\| Float
\|
\| byte
\| Byte
\|
\| short
\| Short
\|
Why Are Wrapper Classes Used?
- To use primitives in collections:
Java collections (e.g., ArrayList
, HashMap
) only work with objects, not primitive types. Wrappers allow you to store primitive values in collections.
2. Example:
java
List<Integer> numbers = new ArrayList<>();
numbers.add(10); // Autoboxing: primitive int is converted to Integer object
3. To take advantage of object methods:
Wrapper classes provide useful methods for working with primitive values, such as parsing strings into numbers and converting between types.
4. Example:
java
int value = Integer.parseInt("100"); // Converts String to int
5. Autoboxing and unboxing:
Java automatically converts between primitive types and their wrapper classes. This is called autoboxing (primitive → wrapper) and unboxing (wrapper → primitive), making the use of wrappers seamless in many cases.
6. Example:
java
Integer num = 10; // Autoboxing: primitive int 10 to Integer
int val = num; // Unboxing: Integer to int
Example:
Without Wrapper Classes:
int num = 5; ArrayList<int> numbers = new ArrayList<>(); // Compilation error: cannot use primitive type
With Wrapper Classes:
Integer num = 5; ArrayList<Integer> numbers = new ArrayList<>(); // Valid: using wrapper class Integer numbers.add(num);
Common Methods in Wrapper Classes:
parseXXX(String s)
:
Converts a string to its corresponding primitive type.
2. Example:
java
int num = Integer.parseInt("123"); // Converts "123" to int 123
3. valueOf(String s)
:
Converts a string into a wrapper object.
4. Example:
java
Integer num = Integer.valueOf("456"); // Converts "456" to Integer object
5. xxxValue()
:
Extracts the primitive value from the wrapper class.
6. Example:
java
int num = numWrapper.intValue(); // Extracts the int value from Integer object
Autoboxing and Unboxing:
- Autoboxing: Automatically converting a primitive to its corresponding wrapper class.
- Example:
java
Integer num = 10; // int to Integer (autoboxing)
* Unboxing: Automatically converting a wrapper class object back to its corresponding primitive.
* Example:
java
int value = num; // Integer to int (unboxing)
Summary:
- Wrapper classes are object representations of primitive types in Java.
- They allow primitives to be used where objects are required, such as in collections.
- They provide useful methods for parsing, conversion, and manipulation.
- Autoboxing and unboxing automatically convert between primitives and their wrapper objects.
In conclusion, wrapper classes in Java bridge the gap between primitive types and objects, allowing primitive data to be used in object-oriented contexts, such as collections, and providing useful utility methods for working with these values.