Professional Documents
Culture Documents
➢ 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
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);
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);
*/
}
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
Integer i2 = Integer.valueOf("1000");
System.out.println(i2);
Character ch = Character.valueOf('a');
System.out.println(ch);
class Test
XxxValue():-
By using XXXValue() method we are converting wrapper objects into the corresponding primitive
values.
XXXValue() Wrapper
Primitive
object
value
Example :-
class Test
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.
Example :-
class Test
{ 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(str);
System.out.println(str1);
Float f = new Float("10.5");
}
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:-
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
AutoBoxing
Primitive Wrapper object
Autounboxing