Wrapper classes are used for converting primitive data types into objects, like int to Integer etc.
Wrapper classes provide a way to use primitive data types (
Wrapper classes provide a way to use primitive data types (
int
, boolean
, etc..) as objects.Primitive | Wrapper class |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
Why we need wrapper class in Java
Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc.
- 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.
- 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..
- An object is needed to support synchronization in multithreading.
Example:
HashMap<Integer, String> hm = new HashMap<Integer, String>();
So for type safety we use wrapper classes. This way we are ensuring that this HashMap keys would be of integer type and values would be of string type.
2. Wrapper class objects allow null values while primitive data type doesn’t allow it.
when working with Collection objects, such as
ArrayList
, where primitive types cannot be used (the list can only store objects):Example
ArrayList<int> myNumbers = new ArrayList<int>(); // Invalid
ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // Valid
Autoboxing: 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.
Wrapper Class Example 1: Converting a primitive type to Wrapper object
public class JavaExample{
public static void main(String args[]){
//Converting int primitive into Integer object
int num=100;
Integer obj=Integer.valueOf(num);
System.out.println(num+ " "+ obj);
}
}
Output:
100 100
As you can see both primitive data type and object have same values. You can use obj in place of num wherever you need to pass the value of num as an object.
Unboxing:
The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.
Wrapper Class Example 2: Converting Wrapper class object to Primitive
public class JavaExample{
public static void main(String args[]){
//Creating Wrapper class object
Integer obj = new Integer(100);
//Converting the wrapper object to primitive
int num = obj.intValue();
System.out.println(num+ " "+ obj);
}
}
Output:
100 100
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the primitive type. To get the value, you can just print the object:
Example
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
Another useful method is the toString()
method, which is used to convert wrapper objects to strings.
In the following example, we convert an Integer
to a String
, and use the length()
method of the String
class to output the length of the "string":
Example
public class MyClass {
public static void main(String[] args) {
Integer myInt = 100;
String myString = myInt.toString();
System.out.println(myString.length());
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.