Wrapper Class :
The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Autoboxing
The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing.
For example:- byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.
-
-
- public class WrapperExample1{
- public static void main(String args[]){
-
- int a=20;
- Integer i=Integer.valueOf(a);
- Integer j=a;
-
- System.out.println(a+" "+i+" "+j);
- }}
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing.
-
-
- public class WrapperExample2{
- public static void main(String args[]){
-
- Integer a=new Integer(3);
- int i=a.intValue();
- int j=a;
-
- System.out.println(a+" "+i+" "+j);
- }}
Comments
Post a Comment