Java Means Durgasoft: DURGA SOFTWARE SOLUTIONS, 202 HUDA Maitrivanam, Ameerpet, Hyd. PH: 040-64512786
Java Means Durgasoft: DURGA SOFTWARE SOLUTIONS, 202 HUDA Maitrivanam, Ameerpet, Hyd. PH: 040-64512786
Java Means Durgasoft: DURGA SOFTWARE SOLUTIONS, 202 HUDA Maitrivanam, Ameerpet, Hyd. PH: 040-64512786
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
1
JAVA Means DURGASOFT
Wrapper Classes:
Collection is an object,it able to store a group of some other Objects.
In Java all the Collection Objects are represented in the form of some predefined
library in "java.util" package.
In java applications,Collection objects never be stored data directly,Collection objects
are able to store data in the form of objects only.
If we want to store data in Collection objects,first we have to convert primitive data
into object type then we have to store object type in Collection object.
If we want to retrieve data from Collection object,first we have to retrieve the
respective object type from Collection then we have to convert data from Object type
to the respective primitive data type.
In the above context,to convert the primitive data into its Object form and to convert
the data from Object form to the respective Primitive data type we have to
use a set of predefined classes called as "Wrapper Classes".
-----Diagram----(CollectionObject.png)
Java has provided all the wrapper classes w.r.t the primitive data types as part of
"java.lang" package.
-------Diagram----(WrapperClassesDiagram.png)
Java has provided all the wrapper classes as immutable classes,whose objects will
not allow modifications on their content.
1.If we want to convert the data from primitive type to its object type or wrapper
type then we have to use the following approaches.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
2
JAVA Means DURGASOFT
To convert the data from primitive type to its respective wrapper type we have to
use the following constructors from each and every wrapper class.
public Byte(byte b)
public Short(short s)
public Integer(int i)
Ex:
int i=10;
System.out.println(i+" "+in);
O/P: 10 10
To convert the data from primitive data type to its object type then each and every wrapper
class has provided the following method.
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
3
JAVA Means DURGASOFT
----
----
Ex:
int i=10;
Integer in=Integer.valueOf(i);
System.out.println(i+" "+in);
O/P: 10 10
To convert the data from primitive type to its corresponding Object type,JDK5.0 version has
given a direct mechanism like to assign primitive variable to the respective wrapper type
variable called as "Auto-Boxing".
Ex:
int i=10;
Integer in=i;
System.out.println(i+" "+in);
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
4
JAVA Means DURGASOFT
O/P: 10 10
2)If we want to convert the data from Wrapper type to primitive type then we have to use
the following approaches:
To convert the data from wrapper type to its respective primitive data type,Each and every
wrapper class has provided the following method.
publicintintValue()
-------------
-------------
Ex:
int i=in.intValue();
System.out.println(in+" "+i);
O/P: 10 10
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
5
JAVA Means DURGASOFT
To convert the data from Wrapper type to primitive type,JDK5.0 version has provided a
direct mechanism like to assign wrapper type variable to the respective primitive type
variable called as "Auto-Unboxing".
Ex:
int i=in;
System.out.println(in+" "+i);
O/P:10 10
3)If we want to convert the data from String data type to wrapper type then we have to use
the following approaches
To convert the data from String type to the respective wrapper type,all the wrapper classes
have provided the following constructors.
Ex:
String data="10";
System.out.println(data+" "+in);
O/P: 10 10
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
6
JAVA Means DURGASOFT
To convert the data from String data type to the respective wrapper type,all most all the
wrapper classes have provided the following method.
Ex:
String data="10";
Integer in=Integer.valueOf(data);
System.out.println(data+" "+in);
O/P: 10 10
4)If we want to convert the data from Wrapper type to String type then we have to use the
following approaches:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
7
JAVA Means DURGASOFT
To convert the data from wrapper type to String data type,allmost all the wrapper classes
have provided the following method.
Ex:
String data=in.toString();
System.out.println(in+" "+data);
O/P: 10 10
In Java,if we concatinate any reference variable with "" then internally JVM will access
toString() method then the over all resultant value is becoming as String.
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
8
JAVA Means DURGASOFT
String data=""+in;
System.out.println(in+" "+data);
O/P: 10 10
5)If we want to convert the data from String data type to the respective primitive data type
then we have to use the following approaches:
To convert the data from String data type to the respective primitive type all most all the
wrapper classes have provided the following method.
------------
------------
Ex:
String data="10";
int i=Integer.parseInt(data);
System.out.println(data+" "+i);
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
9
JAVA Means DURGASOFT
6)If we want to convert the data from primitive data type to String data type then we have
to use the following approaches:
To convert the data from primitive data type to String data type,allmost all the wrapper
classes have provided the following method.
Ex:
int i=10;
String data=Integer.toString(i);
System.out.println(i+" "+data);
O/P: 10 10
If we concatenate any primitive variable with "" then that primitive variable value is
becoming as String value.
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
10
JAVA Means DURGASOFT
int i=10;
String data=""+i;
System.out.println(i+" "+data);
O/P: 10 10
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
11
JAVA Means DURGASOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
12