You are on page 1of 7

Wrapper classes

➢ To represent primitive data types as a Object form we required some classes these classes are
called wrapper classes.
➢ All wrapper classes present in the java.lang package.
➢ We are having 8 primitive data types hence sun peoples are providing 8 wrapper classes.
Wrapper classes hierarchy:-
Object

Number Character Boolean

Byte Short Integer Long Float Double

Constructors of wrapper classes:-

To create objects of wrapper classes All most all wrapper classes contain two constructors but
float contains three constructors & char contains one constructor.
Integer i = new Integer(10);
Integer i1 = new Integer("100");
Float f1= new Float(10.5);

Float f1= new Float(10.5f);


Float f1= new Float("10.5");

Character ch = new Character('a');

Constructor approach to create wrapper Object:-

datatypes wrapper-class constructors


byte Byte byte,String
short Short short,String

int Integer int,String

long Long long,String

float Float double,float,String

double Double double,String

char Character char


boolean Boolean boolean,String

Example :-
class Test
{ public static void main(String[] args)

{ //primitive variables
int a=10;
System.out.println(a);
char ch = 'a';
System.out.println(ch);

//wrapper objects
Integer i1 = new Integer(10);
System.out.println(i1);

Integer i2 = new
Integer("1000");

System.out.println(i2);

Character ch1 = new Character('a');


System.out.println(ch1);//a

/*Integer x = new Integer("ten"); "ten" is unable to convert into integer


System.out.println(x);//NumberFormatException

*/
}

Methods :-
1) valueOf()
2) XXXValue() here xxx = datatype
3) parseXXX() here xxx = datatype
4) toString()
The main importance of wrapper classes:-
1. To convert a data types into the object means we are giving object from data types by using
constructor.
2. To convert String into the data types by using parsexxx() method.
1) valueOf():-
By using valueof() we are creating wrapper object and it is a alternative to the constructor.

Example:-
class Test

{ public static void main(String[] args)


{ //constructor approach create wrapper objects
Integer a1 = new Integer(10);
System.out.println(a1);

Integer a2 = new Integer("100");


System.out.println(a2);

//valueOf() approach to create wrapper Object


Integer i1 = Integer.valueOf(10);
System.out.println(i1);

Integer i2 = Integer.valueOf("1000");
System.out.println(i2);

Character ch = Character.valueOf('a');
System.out.println(ch);

Example :-conversion of primitive to String

class Test

{ public static void main(String[] args)

{ //conversion of primitive to String


int a=10;
int b=20;
String str1=String.valueOf(a);
String str2=String.valueOf(b);
System.out.println(str1+str2);

XxxValue():-
By using XXXValue() method we are converting wrapper objects into the corresponding primitive
values.

XXXValue() Wrapper
Primitive
object
value
Example :-
class Test

{ public static void main(String[] args)


{Integer i=Integer.valueOf(150);
System.out.println("byte value :"+i.byteValue());//-106
System.out.println("short value :"+i.shortValue());//150
System.out.println("int value :"+i.intValue());//150
System.out.println("long value :"+i.longValue());//150

System.out.println("float value :"+i.floatValue());//150.0


System.out.println("double value :"+i.doubleValue());//150.0
Character c=new Character('s');

char ch=c.charValue();
System.out.println(ch);
Boolean b=new Boolean(false);
boolean bb=b.booleanValue();
System.out.println(bb);

}
}

parseXXX():-
By using parseXXX() method we are converting String into the corresponding primitive.

String value parseXXX() Corresponding


primitive

Example :-
class Test

{ public static void main(String[] args)

{ String str1="10";
String str2="20";

System.out.println(str1+str2);//1020
int a=Integer.parseInt(str1); //converting String into integer value
float f=Float.parseFloat(str2); //converting String into float value
System.out.println(a+f);//30.0

toString():-
➢ It prints String representation of Object.
➢ Always The toString() method return type is String Object.
class Test
{ public static void main(String[] args)
{ Test t =new Test();

System.out.println(t.toString());//Object class toString is executed

String str ="ratan";

System.out.println(str.toString());//String class toString is executed

StringBuffer sb = new StringBuffer("ratan");


System.out.println(sb.toString()); //StringBuffer class toString is executed

Integer i = new Integer(100);

System.out.println(i.toString()); //Integer class toString is executed

//Integer toString returns String class Object


String strObject = i.toString();
System.out.println(strObject);

Example :- all classes toString( ) method returns String class object.


class Test
{ public static void main(String[] args)
{ Integer i = new Integer(10);

String str = i.toString(); //returns String class Object

System.out.println(str);

StringBuffer sb = new StringBuffer("ratan");


System.out.println(i.toString());

String str1 = sb.toString(); //returns String class Object

System.out.println(str1);
Float f = new Float("10.5");

String str2 = f.toString(); //returns String class Object


System.out.println(str2);
}

}
Example :-
1) When we print reference variables internally it calls toString() method.
2) On primitive variables we are unable to call toString() method if we are trying
to call toString()compiler generate error message “int cannot be dereferenced”.

class Test
{
public static void main(String[]
args)
{
int a=10;

System.out.println(a.toString());
Integer i = new
Integer(100);

System.out.println(i.toString());
}
}

Compilation
error:-
D:\morn11>j
avac
Test.java
Test.java:6: int cannot be
dereferenced
System.out.printl
n(a.toString());
Autoboxing and Autounboxing:-(introduced in the 1.5 version)
Until 1.4 version we are not allowed to place primitive in the palc wrapper and wrapper
in the place of primitive. The programmer is responsible person to do the explicit
conversion primitive to the wrapperand wrapper to the primitive.
Autoboxing:-

Integer i=10; System.out.println(i);


The above statement does not work on the 1.4 and below versions. The auto
conversion of theprimitive into the Wrapper object is called the autoboxing these
conversions done by compiler at the time of compilation.
Autounboxing:-

int a=new Integer(100); System.out.println(a);

The auto conversion of the wrapper object to the primitive value is called
autounboxing andthese conversions are done by compiler at the time of
compilation.
Example:-

class Test

public static void main(String[] args)

//autoboxing [internal conversion of primitive to wrapper object]

Integer i = 100; System.out.println(i);

//autounboxing [internal conversion of wrapper object to primitive]

int a = new Integer(1000); System.out.println(a);

Automatic conversion of the primitive to wrapper and wrapper to the primitive:-

AutoBoxing
Primitive Wrapper object

Autounboxing

You might also like