In this comprehensive article, we will explore Wrapper Class in Java. We will learn about various types of Wrapper Classes in Java and features of Wrapper Classes. So. let’s get started.
What is Wrapper Class in Java ?
A Wrapper Class in Java is a class that wraps a value of the primitive type in an object.
For Example: The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
Types of Wrapper Class in Java
There are eight primitive data types in Java, and each has a corresponding wrapper class:
Why we need Wrapper Class in Java ?
- Java Collections Framework only work with Objects. So, we can only use Wrapper class objects in Collections.
- Generics: Java’s generics works only with reference types, not primitive types. So, if you need to create a generic class that can handle both objects and primitives, using wrapper classes is a common solution.
- Wrapper class objects allow null values while primitive data type doesn’t allow it.
- An object is needed to support synchronization in multithreading.
- Serialization and Deserialization: When serializing objects to send over networks or save to files, wrapper classes ensure that primitive values are properly serialized and deserialized.
- Parsing and Formatting: Wrapper classes provide methods for parsing Strings into primitive values and formatting values as Strings.
Primitive to Wrapper Class Conversion and Vice Versa
Primitive To Wrapper conversion is known as Boxing.
Integer wrapper = new Integer(5);
Wrapper To Primitive conversion is known as Unboxing.
int i = new Integer(1);
For more in-depth information, please read Boxing and Unboxing
Features of Wrapper Class in Java
Wrapper classes in Java provide several features and functionalities that enhance the flexibility and versatility of working with primitive data types. Here are some key features of wrapper classes:
- Conversion and Parsing: Wrapper classes facilitate conversion between primitive values and strings, as well as parsing strings to obtain primitive values using methods like
valueOf()
andparseXxx()
(e.g.,Integer.valueOf("42")
orDouble.parseDouble("3.14")
). - Autoboxing and Unboxing: Java supports automatic conversion between primitive types and their corresponding wrapper classes, a process known as autoboxing (e.g.,
int
toInteger
) and unboxing (e.g.,Integer
toint
). This simplifies code and enhances readability. - Methods for Arithmetic and Comparison: Wrapper classes provide methods for arithmetic operations (e.g.,
Integer.sum()
,Double.max()
) and comparison (e.g.,Integer.compare()
), which are helpful when working with numeric values. - Type Checking and Conversion: Wrapper classes offer methods like
instanceof
to check the type of an object, enabling safer type checking when dealing with objects. They also enable type conversion methods (e.g.,intValue()
,doubleValue()
) for easy transition between primitives and objects. - Value Constants: Some wrapper classes provide constants, like
Integer.MAX_VALUE
,Integer.MIN_VALUE
, which define the upper and lower bounds of the data type. - Format Conversion: Wrapper classes include methods for formatting and converting values to strings (e.g.,
Integer.toString()
,Double.toHexString()
), making it easier to present data in a readable format. - Representation of Special Values: Wrapper classes can represent special values like
null
using their corresponding objects (e.g.,Integer
can benull
, while primitiveint
cannot). - Compatibility with Collections and Generics: Wrapper classes can be used in collections and with generics, where only objects (not primitives) are allowed. For example,
ArrayList<Integer>
is valid, butArrayList<int>
is not. - Improved Functionality in APIs: Many Java libraries and APIs expect objects rather than primitives. Using wrapper classes allows seamless integration with these APIs.
- Immutable: Wrapper classes are immutable, meaning their values cannot be changed after they are created. This is especially useful for scenarios where you want to ensure data consistency.
- Null Safety: Wrapper classes can hold a
null
value, which can be useful in situations where you need to represent an absence of data. - Support for Special Characters: The
Character
wrapper class allows you to work with special characters like\n
,\t
, etc., providing better support for text manipulation.
Conclusion: Wrapper Class in Java
In this extensive article on “Wrapper Class in Java”, we explore Wrapper Classes, learnt about various types of Wrapper Classes and looked into key features of Wrapper Classes.