Beruflich Dokumente
Kultur Dokumente
Wrapper Class
8050673080
Definition :The class which is used to Wrap the primitive data Types into the object of
that class ,for each such primitive data types there is a particular class dedicated
to it , such classes are termed as Wrapper Classes.
Or
In simple term we can say that A class whose object wraps or contains a
primitive data types.
Or
A wrapper class is defined as a class in which a primitive value is wrapped up.
These primitive wrapper classes are used to represent primitive data type values
as objects. The Java platform provides wrapper classes for each of the primitive
data types
Below Table shows the list of primitive data types and their corresponding
wrapper classes available in Java.
Primitive data type
Wrapper class
Constructor Argument
byte
Byte
Byte or String
short
Short
Short or String
int
Integer
int or String
long
Long
long or String
float
Float
float or String
double
Double
double or String
char
Character
char
boolean
Boolean
boolean or String
KEERTHANA TECHNOLOGIES
8050673080
object:- so that the primitives can be included in activities reserved for objects,
like being added to Collections, or returned from a method with an object
return value
2) The classes in java.util package handle only objects and hence wrapper
classes help in this case also.
3) Wrapper class convert primitive data types into objects which are frequently
used over internet for communication between two applications.
4) Null value is also possible due to wrapper classes ,it also gets included in
collection and Polymorphism and plays an important role.
Example:-When wrappers are used would be in Collections, you can have
an ArrayList<Integer>, but not an ArrayList<int> same
with HashMaps,Hashset etc. To get type safety we use generics and
generics need objects not primitives.
Byte Class
Double Class
Boolean Class
Character Class
5) Float Class
6) Short class
7)Integer Class
8) Long Class
KEERTHANA TECHNOLOGIES
8050673080
Fig:- To demonstrate the primitive data types and their corresponding Wrapper
Classes.
KEERTHANA TECHNOLOGIES
8050673080
1) BYTE ClASS
The Byte class wraps a value of primitive type byte in an object.The Byte class
contains a byte type field.
Constructor of Byte Class:Two Type of constructors are there in Byte class:1) Byte(byte numb);
It takes byte number as its parameter and converts it into Byte class
object
2) Byte (String str);
It takes a String type parameter and converts that string into Byte class
Object
Program 1):-
O/p:1st constructor
12
2nd constructor
109
package wrapperr;
import java.io.*;
public class Longg1
{
public static void main(String[]
args)throws IOException
{ byte s=12;
Byte b1= new Byte(s);
System.out.println("1st
constructor");
System.out.println(b1);
Byte b2= new Byte("109");
System.out.println("2nd
constructor");
System.out.println(b2);
}
KEERTHANA TECHNOLOGIES
8050673080
Uses
To compare the value of two Byte class object and return 0,ve,+ve value
Compares the Byte objects with any other Object obj .If both have
same contents it returns 0 otherwise returns false
It returns Byte equivalent of String str
It converts Byte object into a String object and return the String
object
converts a byte b that contains a byte value into Byte object and
return that object
converts Byte object its corresponding primitive byte type
O/P:Boxing Done
10
23
100
10
10
--------------Unboxing Done
23
100
90
23
KEERTHANA TECHNOLOGIES
8050673080
2)Short Class : Short class wraps a value of primitive data type short in its Object .Short
class object contains a short type field that stores a short number.
Constructor:
package wrapperr;
a) Short(short numb);
b) Short(String str);
Program 3)
short s=10;
Short S1= new Short(s);
String str="Rajeev";
Short S2= new
Short(str);
System.out.println(S1);
System.out.println(S2);
}}
Method Name
int compareTo(Short obj)
boolean equals(object obj)
static short parseBoolean(String str)
String toString()
static short valueOf(String str)
Uses
To compare the numerical value of two short class object
and return 0,-ve,+ve value
Compares the Short objects with any other Object obj .If
both have same contents it returns 0 otherwise returns
false
It returns short equivalent of String str
It converts Short object into a String object and return the
String object
converts a string str that contains a short value into Short
object and return that object
KEERTHANA TECHNOLOGIES
8050673080
O/P:Boxing Done
10
1000
1000
10
10
--------------Unboxing Done
10
1000
9090
10
short t2=S1.shortValue();
short t3=Short.parseShort(str);
short t4=Short.parseShort("9090");
short t5=S1;
System.out.println("Unboxing Done");
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
System.out.println(t5);
}}
KEERTHANA TECHNOLOGIES
8050673080
O/P:10
10
10
10
0
true
10
short t2=S1.shortValue();
short t3=Short.parseShort(str);
short t4=Short.parseShort("8080");
short t5=S1;
System.out.println("Unboxing Done");
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
System.out.println(t5);
}}
KEERTHANA TECHNOLOGIES
8050673080
3 ) Integer Class:The Integer class wraps a value of primitive type int in an object .The integer class Object
conatins an int type field.In this field we can store a primitive int number.
}
}
KEERTHANA TECHNOLOGIES
8050673080
Uses
To compare the numerical value of two Integer class object and return
0,-ve,+ve value
Compares the integer objects with any other Object obj .If both have
same contents it returns 0 otherwise returns false
It returns int equivalent of String str
It converts Integer object into a String object and return the String
object
converts a string str that contains a int value into Short object and
return that object
converts integer object int its corresponding primitive int type
package wrapperr;
KEERTHANA TECHNOLOGIES
8050673080
int k2=i1;
int k3=i1.intValue();
int k4=Integer.parseInt(s1);
int k5=Integer.parseInt("89898");
System.out.println("unboxing being done");
System.out.println(k2);
System.out.println(k3);
System.out.println(k4);
System.out.println(k5);
}}
O/P:-
100
10000
-1
0
false
true
100
KEERTHANA TECHNOLOGIES
8050673080
4 ) Long Class :The Long class contains a primitive long type data .The object of Long class
contains a field where we can store a long value
Program 9 -
package wrapperr;
public class Longg1
{
public static void main(String[] args)
{
Long l1= new Long(1000l);
System.out.println("1st type constructor");
System.out.println(l1);
Long l2= new Long("100898");
System.out.println("2nd type constructor");
System.out.println(l2);
O/P :-
}
}
KEERTHANA TECHNOLOGIES
8050673080
Uses
To compare the numerical value of two Long class object and return
0,-ve,+ve value
Compares the Long objects with any other Object obj .If both have
same contents it returns 0 otherwise returns false
It returns Long equivalent of String str
It converts Long object into a String object and return the String
object
converts a string str that contains a long value into Long object and
return that object
converts integer object long its corresponding primitive long type
O/p:100
10000
-1
0
false
true
100
}}
KEERTHANA TECHNOLOGIES
8050673080
O/P:Boxing Done
7979
10
1000
1000
10
10
--------------Unboxing Done
10
1000
9090
10
long t2=l1.longValue();
long t3=Long.parseLong(str);
long t4=Long.parseLong("9090");
long t5=l1;
System.out.println("Unboxing Done");
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
System.out.println(t5);
}}
KEERTHANA TECHNOLOGIES
8050673080
5 ) Float Class :The Float class wraps a value of primitive type float in an Object .The float class
Object contains a float type field that stores a primitive float number.
Program 12:-
O/P:-
package wrapperr;
public class Float1
{
public static void main(String[] args)
{
Float f1= new Float(10.5f);
System.out.println("1st type constructor");
System.out.println(f1);
Float f2= new Float(10.89898);
System.out.println("2nd type constructor");
System.out.println(f2);
Float f3= new Float("100.098");
System.out.println("3rd type constructor");
System.out.println(f3);
}
}
KEERTHANA TECHNOLOGIES
8050673080
Uses
To compare the numerical value of twoFloat class object and return
0,-ve,+ve value
Compares the Float objects with any other Object obj .If both have
same contents it returns 0 otherwise returns false
It returns Float equivalent of String str
It converts Float object into a String object and return the String
object
converts a string str that contains a Float value into Float object and
return that object
converts Float object its corresponding primitive float type
O/P:-
10.89
1989.78
-1
0
false
true
10.89
}}
KEERTHANA TECHNOLOGIES
8050673080
O/P:-
Boxing Done
797.9
10.0
1000.0
1000.98
10.0
10.0
--------------Unboxing Done
10.0
1000.0
9090.909
10.0
float t1=10.00f;
Float l=new Float(797.90f);
Float l1= new Float(t1);
String str= "1000";
Float l2= new Float(str);
Float l3= new Float("1000.98");
Float l4=Float.valueOf(t1);
Float l5=t1;
System.out.println("Boxing Done");
System.out.println(l);
System.out.println(l1);
System.out.println(l2);
System.out.println(l3);
System.out.println(l4);
System.out.println(l5);
System.out.println("---------------");
float t2=l1.floatValue();
float t3=Float.parseFloat(str);
float t4=Float.parseFloat("9090.909");
Float t5=l1;
System.out.println("Unboxing Done");
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
System.out.println(t5);
}}
KEERTHANA TECHNOLOGIES
8050673080
6 ) Double Class
The double class wraps a value of the primitive type double into an Double class
Object type.Double class contains a double type field which stores primitive
double number
package wrapperr;
public class Double1
{
public static void main(String[] args)
{
Double d1= new Double(10.5);
System.out.println("1st type constructor");
System.out.println(d1);
Double d2= new Double("10.8090");
System.out.println("2nd type constructor");
System.out.println(d2);
}
}
Note :- All methods defined in Double class is similar to that defined in Float
class ,just in place of Float parameter u need to replace it with double type
parameter.(Hope you all got it ). Rest all remain same .
KEERTHANA TECHNOLOGIES
8050673080
7) Boolean Class:
The Boolean class Object contains a primitive boolean type data.The object of
Boolean class contains a field where we can store a boolean value.
Constructor
1)
Boolean(boolean boolValue)
KEERTHANA TECHNOLOGIES
8050673080
Uses
Program 17)
package wrapperr;
O/P :-
String s1="false";
KEERTHANA TECHNOLOGIES
8050673080
O/P :-
true
false
false
}
}
KEERTHANA TECHNOLOGIES
8050673080
O/P:-
true
false
false
true
false
false
}
}
KEERTHANA TECHNOLOGIES
8050673080
O/P:-
String s1="RAJEEV";
Boolean y3= new Boolean(s1);
System.out.println(y1);
true
false
false
true
false
false
true
false
System.out.println(y2);
System.out.println(y3);
boolean x1= y1.booleanValue();
System.out.println(x1);
boolean x2=y2;
Boolean y4=Boolean.valueOf(x2);
String str2=Boolean.toString(true);
String str=y3.toString();
System.out.println(x2);
System.out.println(str);
System.out.println(str2);
System.out.println(y4);
}
}
KEERTHANA TECHNOLOGIES
8050673080
Program 21):- To demonstrate the Boolean Class and its all methods
and constructor and its Boxing and Unboxing
package wrapperr;
O/P:-
1
0
true
false
true
false
false
true
false
false
true
false
false
KEERTHANA TECHNOLOGIES
8050673080
8 ) Character Class:The character class wraps a value of primitive type char into Character class
type Object .Character class contains a char type field where we can store a
primitive char value.
package wrapperr;
Program 22)
Uses
To compare the value of twoCharacter class object and
return 0,-ve,+ve value
Compares the Float objects with any other Object obj .If both
have same contents it returns 0 otherwise returns false
It converts Float object into a String object and return the
String object
converts a char ch that contains a char value into Character
object and return that object
converts Character object its corresponding primitive char
type
KEERTHANA TECHNOLOGIES
8050673080
Program 23) To explain all the methods of Character class in single program.
O/p:Enter a character
12
Rajeev entered:
a digit
Enter a character
Rajeev entered:
a digit
Enter a character
Rajeev entered:
Enter a character
Rajeev entered:
Enter a character
package wrapperr;
import java.io.*;
public class Rajeev1
{
public static void main(String[] args)throws
IOException
{
char ch;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("Enter a character");
ch=(char)br.read();
System.out.println("Rajeev entered:");
if(Character.isDigit(ch))
{
System.out.println("a digit");
}
Enter a character
else if(Character.isUpperCase(ch))
{
System.out.println("an uppercase character");
}
else if(Character.isLowerCase(ch))
{
System.out.println("a lower case character");
}
else if(Character.isSpaceChar(ch))
{
System.out.println("a spacebar character");
}
Rajeev entered:
a spacebar character
Enter a character
Rajeev entered:
Enter a character
Rajeev entered:
Enter a character
Enter a character
D
Rajeev entered:
an uppercase character
Enter a character
Rajeev entered:
Enter a character
Rajeev entered:
Enter a character
else if(Character.isLetter(ch))
{
System.out.println("is letter ");
}
}
}