You are on page 1of 26

KEERTHANA TECHNOLOGIES

Wrapper Class

8050673080

By:- Rajeev Kumar

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

Why Do we need Wrapper Classes ?

Reasons:1) Wrapper classes provides a mechanism to "wrap" primitive values in an

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.

5) It deals with security of Data .


6) By default we know that primitive data types are by default can be passed
bby value ,so in certain criteria we need these primitive data types to be
used as passed by reference ,such solution can be obtained with the help of
wrapper class.

Types Of Wrapper Classes:1)


2)
3)
4)

Byte Class
Double Class
Boolean Class
Character Class

5) Float Class
6) Short class
7)Integer Class
8) Long Class

KEERTHANA TECHNOLOGIES

8050673080

Boxing :- Coverting primitive data types into corresponding Wrapper


rapper
types is called Boxing.
Unboxing :- Converting Wrapper type into Primitive data type is
termed as Unboxing

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

Methods Of Byte Class:Method Name


int compareTo(Byte obj)
boolean equals(object obj)
static Byte parseByte(String str)
String toString()
static Byte valueOf(byte b)
Byte byteValue()

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

Program 2) :- To demonstrate the various ways of


Boxing and Unboxing in Byte class
package wrapperr;
import java.util.*;

O/P:Boxing Done
10
23
100
10
10
--------------Unboxing Done
23
100
90
23

public class Short1


{
public static void main(String[] args) {
byte t1=10;
Byte l=new Byte(t1);
Byte l1= new Byte("23");
String str= "100";
Byte l2= new Byte(str);
Byte l4=Byte.valueOf(t1);
Byte l5=t1;
System.out.println("Boxing Done");
System.out.println(l);
System.out.println(l1);
System.out.println(l2);
System.out.println(l4);
System.out.println(l5);
System.out.println("---------------");
byte t2=l1.byteValue();
byte t3=Byte.parseByte(str);
byte t4=Byte.parseByte("90");
byte 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

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);

public class Short1


{public static void
main(String[] args) {

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);

}}

Methods of Short Class:-

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

Program 4) - To Explain the different ways of Boxing and Un Boxing ,use


of Constructor .
package wrapperr;
import java.util.*;
public class Short1
{
public static void main(String[] args) {
short t1=10;
Short S1= new Short(t1);
String str= "1000";
Short S2= new Short(str);
Short S3= new Short("1000");
Short S4=Short.valueOf(t1);
Short S5=t1;
System.out.println("Boxing Done");
System.out.println(S1);
System.out.println(S2);
System.out.println(S3);
System.out.println(S4);
System.out.println(S5);
System.out.println("---------------");

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

Program 5): to demonstrate different methods used in Short


Class:package wrapperr;
import java.util.*;
public class Short1
{
public static void main(String[] args) {
short t1=10;
Short S1= new Short(t1);
String str="9098";
Short S2= new Short(str);
Short S3= new Short("9090");
Short S4=Short.valueOf(t1);
Short S5=t1;
System.out.println("Boxing Done");
System.out.println(S1);
System.out.println(S2);
System.out.println(S3);
System.out.println(S4);
System.out.println(S5);
System.out.println("---------------");

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.

Constructors of Integer Class:


Integer class contains two constructor,
1) Integer(int num);
It takes int number as its parameter and converts it into Integer class object
2) Integer(String str);
It takes a String type parameter and converts that String into Integer class
object.
Example:- Use of constructors :Program 6)
package wrapperr;
O/P:1st type of constructor
100
2nd type constructor
10000

public class Stringg1


{
public static void main(String[] args) {
System.out.println("1st type of
constructor");
Integer i1= new Integer(100);
System.out.println(i1);
Integer i2= new Integer("10000");
System.out.println("2nd type constructor");
System.out.println(i2);

}
}

KEERTHANA TECHNOLOGIES

8050673080

Methods Of Integer Class:


Method Name
int compareTo(Integer obj)
boolean equals(object obj)
static int parseInt(String str)
String toString()
static Integer valueOf(String str)
int intValue()

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

Program 7 )- To explain various ways of Boxing and Unboxing in


Integer class .

package wrapperr;

O/p:Boxing getting done


100
10000
90
90
90
90
89898
89898
unboxing being done
100
100
89898
89898

public class Stringg1


{
public static void main(String[] args) {

Integer i1= new Integer(100);


Integer i2= new Integer("10000");
int k1=90;
Integer i3=new Integer(k1);
Integer i4=Integer.valueOf(k1);
Integer i5=Integer.valueOf(90);
Integer i6=k1;
String s1="89898";
Integer i7=new Integer(s1);
Integer i8=new Integer("89898");
System.out.println("Boxing getting done");
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
System.out.println(i6);
System.out.println(i7);
System.out.println(i8);

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);

}}

Pgm 8:- To demonstrate various methods ,constructor of Integer Class


in single program.
package wrapperr;
public class Stringg1
{
public static void main(String[] args) {

O/P:-

Integer i1= new Integer(100);

100
10000
-1
0
false
true
100

Integer i2= new Integer("10000");


Integer i3= new Integer(100);
int k=i1.compareTo(i2);
int k2=i1.compareTo(i3);
boolean b1=i1.equals(i2);
boolean b2=i1.equals(i3);
String str=Integer.toString(i1);
int k3=Integer.parseInt(str);
System.out.println(i1);
System.out.println(i2);
System.out.println(k);
System.out.println(k2);
System.out.println(b1);
System.out.println(b2);
System.out.println(k3);
}
}

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

Constructors of Long Class:There are two constructor in Long class


1) Long(long numb);
It takes long number as its parameter and converts it into Long class Object
2) Long(String str);
This constructor takes String as its parameter and converts it into its
corresponding Long class object.

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 :-

1st type constructor


1000
2nd type constructor
100898

}
}

KEERTHANA TECHNOLOGIES

8050673080

Methods of Long Class:Method Name


int compareTo(long obj)
boolean equals(object obj)
static Long parseInt(String str)
String toString()
static Long valueOf(String str)
long longValue()

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

Program 10): To reflect different methods ,constructors


being used in single program of Long Class.
package wrapperr;
public class Long1
{
public static void main(String[] args) {

Long i1= new Long(100L);


Long i2= new Long("10000");
Long i3= new Long(100);
int k=i1.compareTo(i2);
int k2=i1.compareTo(i3);
boolean b1=i1.equals(i2);
boolean b2=i1.equals(i3);
String str=Long.toString(i1);
int k3=Integer.parseInt(str);
System.out.println(i1);
System.out.println(i2);
System.out.println(k);
System.out.println(k2);
System.out.println(b1);
System.out.println(b2);
System.out.println(k3);

O/p:100
10000
-1
0
false
true
100

}}

KEERTHANA TECHNOLOGIES

8050673080

Program 11 :- To describe various ways of Boxing and Unboxing ,and


Constructor in single Progam of Long Class
package wrapperr;
import java.util.*;
public class Short1
{
public static void main(String[] args) {
long t1=10;
Long l=new Long(7979l);
Long l1= new Long(t1);
String str= "1000";
Long l2= new Long(str);
Long l3= new Long("1000");
Long l4=Long.valueOf(t1);
Long 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("---------------");

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.

Constructor of Float class


There are three types of constructor :1) Float(float numb);
It takes float number as its parameter and converts it into Float class
object
2) Float(double numb);
It takes double type number as parameter and converts it it to Float class
type Object
3) Float(String str);
It takes String type as parameter and converts that String into Float class
Objects.

Program 12:-

O/P:-

1st type constructor


10.5
2nd type constructor
10.89898
3rd type constructor
100.098

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

Methods of Float Class:


Method Name
int compareTo(Float obj)
boolean equals(object obj)
static Long parseFloat(String str)
String toString()
static Float valueOf(String str)
Float floatValue()

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

Program 13):- To describe various methods of Float Class along with


constructor in single Program
package wrapperr;

O/P:-

public class Floatt1


{
public static void main(String[] args) {

10.89
1989.78
-1
0
false
true
10.89

Float f1= new Float(10.89f);


Float f2= new Float("1989.78f");

Float f3= new Float(10.89f);


int k=f1.compareTo(f2);
int k2=f1.compareTo(f3);
boolean b1=f1.equals(f2);
boolean b2=f1.equals(f3);
String str=Float.toString(f1);
float k3=Float.parseFloat(str);
System.out.println(f1);
System.out.println(f2);
System.out.println(k);
System.out.println(k2);
System.out.println(b1);
System.out.println(b2);
System.out.println(k3);

}}

KEERTHANA TECHNOLOGIES

8050673080

Program 14):- To explain various ways of Boxing and


Unboxing in Float Class
package wrapperr;
import java.util.*;

O/P:-

public class Rajeev11


{
public static void main(String[] args) {

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

Constructor of Double Class


Two constructors are present in Double class:
1) Double (double numb);
2) Double (String str);

Program 15)O/p:1st type constructor


10.5
2nd type constructor
10.809

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)

Here boolValue must be either true or false


Example :
Boolean b1= new Boolean(true);
Boolean b2= new Boolean(false);
Or
boolean x1=true;
Boolean b3=new Boolean(x1);
boolean x2 =false;
Boolean b4=new Boolean(x2);
2)Boolean(String boolString)
Here if boolString contains the string true (in uppercase or lowercase),
then the new Boolean object will be true. Otherwise, it will be false.
Example:String s1= true;
Boolean b1= new Boolean(s1);
Boolean b2= new Boolean(false);

KEERTHANA TECHNOLOGIES

8050673080

Important Methods of Boolean class:


Method Name

Uses

int compareTo(Boolean obj)

To compare the numerical value of two Boolean


class object and return 0,-ve,+ve vlaue
Compares the Boolean objects with any other
Object obj .If both have same contents it returns
0 otherwise returns false
It returns boolean equivalent of String str
It converts Boolean object into a String object and
return the String object
converts a string str that contains a boolean value
into Boolean object and return that object

boolean equals(object obj)

static boolean parseBoolean(String str)


String toString( )
static boolean valueOf(String str)

Program 17)
package wrapperr;

O/P :-

public class Rajeev1


true
false
false

public static void main(String[] args)


{

Boolean b1= new Boolean("true");

Boolean b2= new Boolean("false");

String s1="false";

KEERTHANA TECHNOLOGIES

8050673080

Program 18): Explaining the constructor of Boolean class with


the help of program
package wrapperr;
public class Rajeev1
{
public static void main(String[] args)
{
Boolean b1= new Boolean("true");

O/P :-

Boolean b2= new Boolean("false");

true
false
false

String s1="RAJEEV"; // here name is


stored as String and been passed as
argument
Boolean b3= new Boolean(s1);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);

}
}

KEERTHANA TECHNOLOGIES

8050673080

Program 19):- To demonstrate different ways of boxing and Unboxing


of boolean type
package wrapperr;
public class Rajeev3
{
public static void main(String[] args)
{
Boolean b1= new Boolean("true");
Boolean b2= new Boolean("false");
String s1="RAJEEV";
Boolean b3= new Boolean(s1);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);

O/P:-

true
false
false
true
false
false

boolean x1= b1.booleanValue();


System.out.println(x1);
boolean x2=b2;
String str=b3.toString();
System.out.println(x2);
System.out.println(str);

}
}

KEERTHANA TECHNOLOGIES

8050673080

Program 20): To demonstrate some more ways of Boxing


and AutoBoxing of Boolean class type
package wrapperr;
public class Rajeev1
{
public static void main(String[] args)
{
Boolean y1= new Boolean("true");
Boolean y2= new Boolean("false");

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

public class Rajeev1


{
public static void main(String[] args)
{
Boolean y1= new Boolean("true");
Boolean y2= new Boolean("false");
Boolean y7= new Boolean("false");
int k=y1.compareTo(y2);
int l=y2.compareTo(y7);
boolean h1=y2.equals(y7);
boolean h2=y1.equals(y2);
System.out.println(k);
System.out.println(l);
System.out.println(h1);
System.out.println(h2);
String s1="RAJEEV";
Boolean y3= new Boolean(s1);
System.out.println(y1);
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);
boolean x5=Boolean.parseBoolean(s1);
String str=y3.toString();
System.out.println(x2);
System.out.println(str);
System.out.println(str2);
System.out.println(y4);
System.out.println(x5);
}
}

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;

Constructor Of Character Class:In character class we


Character(char ch);
Example:- Character

public class Longg1


{
public static void main(String[] args)
have only one constructor{
Character c= new Character('x');
char s='z';
Character c2=new Character(s);
System.out.println(c);
System.out.println(c2);
c= new Character(x);
}

Program 22)

Methods of Character Class:Method Name


int compareTo(Char obj)
boolean equals(object obj)
String toString()
static Character valueOf(char ch)
Char charValue()

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 ");
}
}
}