Date: july-03-2016
Wrapper classes:-
Collection is an object, it able to store a group of other
objects.
In java applications, collection objects are not storing
primitive data directly., collection object is able to store the object
reference values.
If we want to store primitive data in collection object,
first, we have to convert data from primitive type to object type then we have
to store the generated object in collection object.
In the above context, if we want to get primitive data from
collection object, first, we have to get data in the form of object then, we
have to convert data from object type to primitive type.
Conclusion:-
In the above two situations, to convert data from primitive
type to object type and to convert data
from object type to primitive type, we have to use a set of predefined classes
called as “Wrapper classes”.
Java has provide the following list of wrapper classes with
respect to the 8 number of primitive data types.
|
Primitive
Data Types
|
Wrapper
classes
|
|
byte
|
java.lang.Byte
|
|
short
|
java.lang.Short
|
|
int
|
java.lang.Integer
|
|
long
|
java.lang.Long
|
|
float
|
java.lang.Float
|
|
double
|
java.lang.Double
|
|
char
|
java.lang.Character
|
|
boolean
|
java.lang.Boolean
|
Note:-
All wrapper class objects are immutable objects, they will
not allow modifications on their content, if we trying to perform modifications
over wrapper class object context then data is allowed for operations, but, the
respective data will not be stored back in original object, the resultant data
will be store by creating new object.
1.
Conversion from primitive data type to object
type:-
a.
By using parameterized constructors.
public XXX(xxx value)
xxx---->byte, short, int,.....
XXX---->Wrapper classes.
Ex :-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=new Integer(i);
System.out.println(i+"
"+in);
}
}
Output:-
10 10
b.
By using valueOf() method:-
public static XXX valueOf(xxx val)
xxx--->byte,short,int,....
XXX---->Wrapper classes
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=Integer.valueOf(i);
System.out.println(i+"
"+in);
}
}
Output:-
10 10
c.
By using Auto-Boxing mechanism:-
Auto-Boxing mechanism by JDK5.0 version, in this approach to convert data
from primitive type to object type no need to use any predefined method and
constructors. Simply, we have to assign
primitive variable to the respective wrapper class reference variable.
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=i;
System.out.println(i+"
"+in);
}
}
Output:-
10 10
2.
Conversion from object type to Primitive
type:-
a.
By using xxxValue() method from wrapper
classes:-
public xxx xxxValue()
xxx--->byte,short,....
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
int
i=in.intValue();
System.out.println
in+" "+i);
}
}
Output:-
10 10
b.
By using auto unboxing mechanism:-
To conversion data from object type to primitive type, if we use
auto-unboxing mechanism then it is not required to use any predefined library,
simply, we have to assign wrapper class reference variable to the respective
primitive variable.
Note:-
Auto-Unboxing feature was provided by JAVA along with its JDK 5.0
Version.
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
int i=in;
System.out.println(in+"
"+i);
}
}
Output:-
10 10
3.
Conversion from String type to Object type:-
a.
By using String parameterized constructor:-
public XXX(String data)
XXX --->Wrapper classes.
Ex code:-
class Test
{
public static void
main(String[] args)
{
String
data="10";
Integer
in=new Integer(data);
System.out.println(data+"
"+in);
}
}
Output:-
10 10
b.
By using static valueOf(-)method:-
public static XXX valueOf(String data)
XXX --->Wrapper classes.
Ex code:-
class Test
{
public static void
main(String[] args)
{
String
data="10";
Integer
in=Integer.valueOf(data);
System.out.println(data+"
"+in);
}
}
Output:-
10 10
4.
Conversion from Object type to String type:-
a.
By using toString() method:-
public String toString()
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
String
data=in.toString();
System.out.println(in+"
"+data);
}
}
Output:-
10 10
b.
By using ‘+’ concatenation operator:-
In java applications, if we concatenate any reference variable with “ ”
then JVM will access toString() method internally and JVM will appends the
return value of toString() to “”.
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
String
data=""+in;
System.out.println(in+"
"+data);
}
}
Output:-
10 10
5.
Conversion from primitive type to String
type:-
a.
By using static toString() method from all
the Wrapper classes:-
public static String toString(xxx value)
xxx--->byte, short, int, ....
Ex code:-
class Test
{
public static void main(String[]
args)
{
int i=10;
String
data=Integer.toString(i);
System.out.println(i+"
"+data);
}
}
Output:-
10 10
b.
By using ‘+’ concatenation operator :-
In java applications, if we concatenate any primitive variable to “ “ by using ‘+’ operator then the primitive
variable data will be converted to String type.
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
String
data=""+i;
System.out.println(i+"
"+data);
}
}
Output:-
10 10
6.
Conversion from String type to Primitive
type:-
a.
To convert data from String type to primitive
type then we have to use the following method.
public static xxx parseXXX(String data)
xxx--->byte, short, ....
Ex code:-
class Test
{
public static void
main(String[] args)
{
String data="10";
int
i=Integer.parseInt(data);
System.out.println(data+"
"+i);
}
}
Output:-
10
10
![]() |


No comments:
Post a Comment