in this wrapper classes in java tutorial, we are learning about Wrapper Classes in java. We have discussed about the Primitive Data Types in previous tutorial . Since the Primitive Data Types can't be directly used as objects that's why Wrapper classes come into picture.
Generic classes work with objects. but it don't support Primitives.
Introduction to Wrapper Classes in Java
Wrapper classes are required as they convert primitive data types into objects and objects are really important if we need to change the arguments passed in a method.
let's discuss in detail about the Wrapper Classes in java.
The Java programming language come up with the java.lang package, which has classes that are basic to the design and the most important classes among them are Object and Class.
So, Java wrapper classes wraps the values of primitive data types as an object. When an object is created to a wrapper class, it accommodate a field which can store the primitive data types.
The object of one type contains a field of that specific type only or same type, which means a Double type of object accommodate double type of field only, representing that value so that a reference to it can be stored in a variable of reference type.
Wrapper classes in Java
Wrapper classes provide a way to use primitive data types (int, boolean, char, etc..) as objects.
The wrapper class in Java provides the system to convert primitive into object and object into primitive.
wrapper classes give a way to use primitive data types (int, char, short, byte, etc) as objects. These wrapper classes declare in java.util package.
Since, J2SE 5.0, auto-boxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as auto-boxing and vice-versa unboxing.
Use of Wrapper classes in Java
Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.
Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
Synchronization: Java synchronization works with objects in Multithreading.
java.util package: The java.util package provides the utility classes to deal with objects.
Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.
Need of Wrapper Classes
1. They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.
3. The classes in java.util package handles only objects and hence wrapper classes help in this case also.
4. Data structures in the Collection framework such as ArrayList and Vector store only the objects (reference types) and not the primitive types.
5. The object is needed to support synchronization in multithreading.
6. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
7. An object is needed to support synchronization in multithreading.
there are 8 wrapper classes in java
The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes are given below:
Primitive
Type |
Wrapper
class |
boolean |
Boolean |
char |
Character |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
Implementation of the wrapper class in Java
Autoboxing in Wrapper Class
Autoboxing is used to convert primitive data types into corresponding objects.
Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double etc.
Example
public class AutoBoxingTest {
public static void main(String args[]) {
int num = 99; // int primitive
Integer temp = Integer.valueOf(num); // creating a wrapper class object
System.out.println(num + " " + temp);
}
}
OUTPUT:
java -cp /tmp/KiBmZBjRN0 AutoBoxingTest
99 99
Unboxing in Wrapper Class
Unboxing is used to convert the Wrapper class object into corresponding primitive data types.
It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc.
Example
public class UnboxingTest {
public static void main(String args[]) {
Integer temp = new Integer(99); // Creating Wrapper class object
int num = temp.intValue(); // Converting the wrapper object to primitive datatype
System.out.println(num + " " + temp);
}
}
OUTPUT:
java -cp /tmp/KiBmZBjRN0 UnboxingTest
99 99
Creating Wrapper Objects
Using a Wrapper Class Constructor
We can create a wrapper object using the wrapper class and its constructor by passing the value to it.
ClassName object = new ClassName(argument);
Using Wrapper class only (instead of the primitive type)
Features of Java Wrapper Classes
wrapper class methods in java
Methods Supported by the Wrapper Classes
Method |
Method
Description |
typeValue() |
Converts the value of this Number
object to the specified primitive data type returned |
compareTo() |
Compares this Number object to the
argument |
equals() |
Determines whether this Number object
is equal to the argument |
valueOf() |
Returns an Integer object holding the
value of the specified primitive data type value |
toString() |
Returns a String object representing
the value of specified Integer type argument |
parseInt() |
Returns an Integer type value of a
specified String representation |
decode() |
Decodes a String into an integer |
min() |
Returns the smaller value after
comparison of the two arguments |
max() |
Returns the larger value after
comparison of the two arguments |
round() |
Returns the closest round off long or
int value as per the method return type |
wrapper class in java interview questions
Primitive Type | Wrapper class |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |