Wrapper Classes PDF
Wrapper Classes PDF
Wrapper Classes PDF
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 1
JAVA Means DURGASOFT
Wrapper classes
Java is an Object oriented programming language so represent everything in the form of
the object, but java supports 8 primitive data types these all are not part of object.
To represent 8 primitive data types in the form of object form we required 8 java classes
these classes are called wrapper classes.
All wrapper classes present in the java.lang package and these all classes are immutable
classes.
Wrapper classes hierarchy:-
Object
Note :- To create wrapper objects all most all wrapper classes contain two constructors but
Float contains three constructors(float,double,String) & char contains one constructor(char).
toString():-
toString() method present in Object class it returns class-name@hashcode.
String,StringBuffer classes are overriding toString() method it returns content of the objects.
All wrapper classes overriding toString() method to return content of the wrapper class objects.
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 2
JAVA Means DURGASOFT
Example :-
class Test
{ public static void main(String[] args)
{ Integer i1 = new Integer(100);
System.out.println(i1);
System.out.println(i1.toString());
Example:-
In java we are able to call toString() method only on reference type but not primitive type.
If we are calling toString() method on primitive type then compiler generate error message.
class Test
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 3
JAVA Means DURGASOFT
int a=100;
System.out.println(a);
//System.out.println(a.toString()); error:-int cannot be dereferenced
}
}
valueOf():-
in java we are able to create wrapper object in two ways.
a) By using constructor approach
b) By using valueOf() method
valueOf() method is used to create wrapper object just it is alternate to constructor approach
and it a static method present in wrapper classes.
Example:-
class Test
{ public static void main(String[] args)
{ //constructor approach to create wrapper object
Integer i1 = new Integer(100);
System.out.println(i1);
Integer a2 = Integer.valueOf("1000");
System.out.println(a2);
}
}
class Test
{ public static void main(String[] args)
{ int a=100;
int b=200;
System.out.println(a+b);
Integer a2 = Integer.valueOf("1000");
System.out.println(a2);
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 5
JAVA Means DURGASOFT
}
}
parseXXX():- it is used to convert String into corresponding primitive value& it is a static method present
in wrapper classes.
Example :-
class Test
{ public static void main(String[] args)
{ String str1="100";
String str2="100";
System.out.println(str1+str2);
//parseXXX() converion of String to primitive type
int a1 = Integer.parseInt(str1);
float a2 = Float.parseFloat(str2);
System.out.println(a1+a2);
}
}
Example:-
class Test
{ public static void main(String[] args)
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 6
JAVA Means DURGASOFT
Factory method:-
One java class method returns same class object or different class object is called factory
method.
There are three types of factory methods in java.
o Instance factory method.
o Static factory method.
o Pattern factory method.
The factory is called by using class name is called static factory method.
The factory is called by using reference variable is called instance factory method.
One java class method is returning different class object is called pattern factory method.
Example:-
class Test
{ public static void main(String[] args)
{ //static factory method
Integer i = Integer.valueOf(100);
System.out.println(i);
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 7
JAVA Means DURGASOFT
Runtime r = Runtime.getRuntime();
System.out.println(r);
String s1="sravyainfotech";
String s2 = s1.substring(0,6);
System.out.println(s2);
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 8
JAVA Means DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 9