Search Your Topic

config
Thursday, 13 July 2017 11:08

Wrapper class in Java

Written by
Rate this item
(0 votes)

Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known as autoboxing and vice-versa unboxing.

The eight classes of 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

Wrapper class Example: Primitive to Wrapper

 
  1. public class WrapperExample1{  
  2. public static void main(String args[]){  
  3. //Converting int into Integer  
  4. int a=20;  
  5. Integer i=Integer.valueOf(a);//converting int into Integer  
  6. Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  
  7.   
  8. System.out.println(a+" "+i+" "+j);  
  9. }}  

Output:

20 20 20

Wrapper class Example: Wrapper to Primitive

 
  1. public class WrapperExample2{    
  2. public static void main(String args[]){    
  3. //Converting Integer to int    
  4. Integer a=new Integer(3);    
  5. int i=a.intValue();//converting Integer to int  
  6. int j=a;//unboxing, now compiler will write a.intValue() internally    
  7.     
  8. System.out.println(a+" "+i+" "+j);    
  9. }}    

Output:

3 3 3
Read 181 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook