Introduction
In this article, we will learn to represent all 8 primitive values into an object. The purpose of representing primitive values as objects is sometimes in the project we must send primitive values as arguments to a method whose parameter is java. lang.Object class type.
For Example.
If you want to store primitive data in java collections, we can only store them only considering objects because java documentation says that we can store only objects in collections. So Collections accept only objects. In java, primitive values as an object have no need to define any special classes.
Oracle has given us 8 special primitive classes technically these classes are called Wrapper classes in java.
What are the Wrapper classes?
The class is used to store primitive values in its objects to represent it is an object is called wrapper classes.
Wrapper classes are used to carry primitive values by using an array or collection object without losing data type.
We have 8 wrapper classes for each primitive data type in java to represent them as objects.
Only 6 classes represent the number wrapper classes. Those classes are subclasses of the Number class which is an abstract class in java.
These wrapper classes are available in java.lang package.
Every primitive data type has its corresponding wrapper class.
String class is not a Wrapper class.
Primitive data types & It's Wrapper Classes
It is the mechanism to convert primitive into object and object into primitive.
Autoboxing
The automatic conversion of a primitive into an object is known as autoboxing.
Unboxing
The conversion of objects into primitive data types is called unboxing.
Need for Wrapper Classes
Collection classes only work with objects. They do not support primitive data types. If we need to work we should convert primitive values into their corresponding wrapper classes object.
In Collections ArrayList and Vector classes stores only objects (reference types) and not primitive types.
We should convert primitive to wrapper objects because if we want to work with synchronization in multithreading we need objects because synchronization can not support primitives.
Serialization:
Use of Wrapper classes
Java is an object-oriented programming language, in java, everything is an object except primitives so we need to deal with objects mostly like in Collections, Serialization, Synchronization, etc.
Primitive to Wrapper Class Conversion
Okay, up to now we understood what is primitives, wrapper classes, and the use of all this. So this is the time we will see the programming approach to understand clearly. So let's start.
In primitive to wrapper class conversion we can either use constructor or static factory methods to convert a primitive value to an object of a wrapper class.
In Java 9, Constructors for primitives such as Integer or Long have been deprecated.
It's highly recommended to only use the factory methods on new code.
Example of converting an int value to an Integer object.
Integer object = new Integer(1);
Integer object2 = Integer.valueOf(1);
valueOf() method returns an instance representing the specified int value.
In the same way, we can also convert boolean to Boolean, byte to Byte, char to Character, long to Long, float to Float, and double to Double.
If we have to convert String to Integer then we need to use parseInt() method because String isn't a wrapper class.
To convert from a wrapper object to a primitive value, we can use the corresponding methods such as intValue(), doubleValue().
int val = object.intValue();
Autoboxing
Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing.
Example
Conversion of int to Integer, long to Long, etc.
After Java 5, this conversion can be done automatically by using features called autoboxing and unboxing.
“Boxing” refers to converting a primitive value into a corresponding wrapper object. Because this can happen automatically, it's known as autoboxing.
Similarly, when a wrapper object is unwrapped into a primitive value then this is known as unboxing.
Example of Autoboxing
List<Integer> list = new ArrayList<>();
list.add(1); // autoboxing
Integer val = 2; // autoboxing
Java will automatically convert the primitive int value to the wrapper.
Internally, it uses the valueOf() method for conversion.
For example, the following lines are equivalent:
Integer value = 3;
Integer value = Integer.valueOf(3);
Example
class PrimitiveWrapper{
public static void main(String args[]){
//Converting int into Integer
int a=20;
//converting int into Integer explicitly
Integer i=Integer.valueOf(a);
//autoboxing, compiler write Integer.valueOf(a) internally
Integer j=a;
System.out.println(a+" "+i+" "+j);
}
}
Output : 20 20 20
20 2
Unboxing
Conversion of a wrapper object 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.
For Example
Integer to int, Long to long, Character to char, etc.
class IntegerToint{
public static void main(String args[]){
Integer a=new Integer(3);
int i=a.intValue(); //converting Integer to int explicitly
int j=a; //unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}
}
2 comments
Click here for commentsPlease format it ...other is best
ReplyOkay no problem I think this is interface problem we will sort out very soon.
Reply