Вы находитесь на странице: 1из 6

Wrapper Classes in Java

A Wrapper class is a class whose object wraps or contains a primitive data


types. When we create an object to a wrapper class, it contains a field and in
this field, we can store a primitive data types. In other words, we can wrap a
primitive value into a wrapper class object.
Note:- This is required especially when we need an object reference to a
primitive value because the type of functionality required by their natural
atomic form, such as  int,  char,  double,  Boolean, and so forth, would not
suffice.

1.1 Primitive and Reference Types in Java


1.1.1 Primitive types those that store single declared types of a value at
a time. For example, a variable defined as int can store a whole number at a
time. At an instance when another value is stored, the initial value is replaced
by the new one. In Java, primitive variables are initialized by default. If it is a
numeric type, such as designated by byte, short, int, long, float, and double, it
is initialized to 0 (zero), the char type is initialized to the numeric value of -1
and the Boolean type is initialized to false by default. The initial default value
may be overwritten by assigning a value in its declaration.

int intvalue=102;

Note:- However, note that locally declared primitive values are not initialized
by default. Any attempt to access the local uninitialized variable is a compile-
time error.

1.1.2 All other variables that are not of primitive types are actually
reference types. Reference types hold references to memory objects. Each of
the referenced objects may contain many instance variables. They are
initialized by default to a  null  value, meaning, references nothing or no
memory object. Reference type variables have a class definition with
declared methods and properties. They are instantiated with the help of
constructors invoked via the  new  keyword; typically, the object instance
created is used to access the methods defined within them.

Person person=new Person();


Figure 1: The wrapper classes' hierarchical structure

Wrapper Primitive Description


Class Types
Boolean Boolean This wrapper class is wrapped around
a Boolean type where the Boolean values
are true and false.
Character char This wrapper class is wrapped around a primitive
single field char type. The data in the Character are
defined by the information in the UnicodeData.
Reference: www.unicode.org.
Byte byte This wrapper class is wrapped around a
primitive byte type.
Double double This wrapper class is wrapped around a
primitive double type.
Float float This wrapper class is wrapped around a
primitive float type.
Integer int This wrapper class is wrapped around a
primitive int type.
Long long This wrapper class is wrapped around a
primitive long type.
Short short This wrapper class is wrapped around a
primitive short type.
Void void Wrapper class for void. Unlike other wrapper
classes, this class cannot be instantiated. It simply
signifies the idea of a void reference.

1.2 Importance of Wrapper classes

There are mainly two uses with wrapper classes.


1) To convert simple data types into objects, that is, to give object form to a
data type; here constructors are used.
2)  To convert strings into data types (known as parsing operations), here
methods of type parseXXX() are used.

1.3 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. D a t a s t r u c t u r e s i n t h e C o l l e c t i o n f r a m e w o r k , s u c h
as  ArrayList  and  Vector, store only objects (reference types) and not
primitive types.
4. An object is needed to support synchronization in multithreading.

1.4 Wrapper class Example

1.4.1 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. I n t e g e r  j=a;//
autoboxing, now compiler will write Integer.valueOf(a) internally  
7.   
8. System.out.println(a+" "+i+" "+j);  
9. }}  

Output:

20 20 20

1.4.2 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:

333

class WrappingUnwrapping
{
public static void main(String args[])
{
// byte data type
byte a = 1;
// wrapping around Byte object
Byte byteobj = new Byte(a);
// int data type
int b = 10;
//wrapping around Integer object
Integer intobj = new Integer(b);
// float data type
float c = 18.6f;
// wrapping around Float object
Float floatobj = new Float(c);
// double data type
double d = 250.5;
// Wrapping around Double object
Double doubleobj = new Double(d);
// char data type
char e='a';
// wrapping around Character object
Character charobj=e;
// printing the values from objects
System.out.println("Values of Wrapper objects (printing as objects)");
System.out.println("Byte object byteobj: " + byteobj);
System.out.println("Integer object intobj: " + intobj);
System.out.println("Float object floatobj: " + floatobj);
System.out.println("Double object doubleobj: " + doubleobj);
System.out.println("Character object charobj: " + charobj);
// objects to data types (retrieving data types from objects)
// unwrapping objects to primitive data types
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;
System.out.println("Unwrapped values ");
System.out.println("byte value, bv: " + bv);
System.out.println("int value, iv: " + iv);
System.out.println("float value, fv: " + fv);
System.out.println("double value, dv: " + dv);
System.out.println("char value, cv: " + cv);
}
}

1.5 Autoboxing and Unboxing in Java

1.5.1 Wrapper Class in Java – Autoboxing

Autoboxing in Java is used to convert automatically convert the primitive data


types into corresponding objects.

// Java program to demonstrate Autoboxing


import java.util.ArrayList;
class Autoboxing
{
public static void main(String[] args)
{
char ch = 'a';;
Character a = ch;
ArrayList<Integer> arrayList = new ArrayList<Integer>()
arrayList.add(25);
System.out.println(arrayList.get(0));
}
}

1.5.2 Wrapper Class in Java – Unboxing


Unboxing in Java is reverse of Autoboxing, i.e. it converts wrapper class
object into its corresponding primitive data type.

// Java program to demonstrate Unboxing


import java.util.ArrayList;
class Unboxing
{
public static void main(String[] args)
{
Character ch = 'a';
char a = ch;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(24);
int num = arrayList.get(0);
System.out.println(num);
}
}

1.6 Advantages of Autoboxing and Unboxing in Java

• It helps in writing a clearer note.


• It helps us use the wrapper class and primitive types interchangeably
without the need of typecasting.

Вам также может понравиться