Você está na página 1de 8

Wrapper classes

To make use the Iully obiect orient concept iava introduce the derived obiects oI all primitive
types.
Below is the list oI all wrapper class in iava.
Primitive data type Wrapper class
Byte
Byte
Int
Integer
Double
Double
Long
Long
Boolean
Boolean
Char Character
loat
Float

Wrapper class helps you convert the primitive data type into derived variable and Irom derived
variable to primitive data type.
All the wrapper classes are Iinal and toString() and hashCode() method has been overridden in
order to compare the state oI the obiect.
All the wrapper classes except Character have two constructors. One constructor takes the
respective data type and second constructor takes the string which convert to respective data
type.
Character class has only one constructor which takes only char data type.
Auto boxing and unboxing using wrapper class
Getting the derived obiect Irom a primitive data type is called the boxing.
Getting the primitive data type Irom the derived obiect called unboxing.
Boxing and unboxing is happening automatically using wrapper classes using iava 5 or newer
version.
Example of wrapper classes:
?
1
2
3
public class Wrapperworld

public static void main(String` args)

5
6
7
8
9
10
11
12
13
1
15
16
17
18
19
20
21
22

// Integer Example
int i = 9;
Integer intJbj = new Integer(i);
int j = intJbj.intValue();

// Jne constructor is taking the litrals
Integer intJnj2 = new Integer(10);

// Jne constructor is taking the String
Integer intJnj3 = new Integer("10");

// Jne constructor is taking the litrals
Double d1 = new Double(100.000);

// Jne constructor is taking the String
Double d2s = new Double("100.000");






Wrapper Class
!ava ls an ob[ecLorlenLed lanauaae and as sald evervLhlna ln [ava ls an ob[ecL 8uL whaL abouL Lhe
prlmlLlves? 1hev are sorL of lefL ouL ln Lhe world of ob[ecLs LhaL ls Lhev cannoL parLlclpaLe ln Lhe ob[ecL
acLlvlLles such as belna reLurned from a meLhod as an ob[ecL and belna added Lo a CollecLlon of
ob[ecLs eLc As a soluLlon Lo Lhls problem !ava allows vou Lo lnclude Lhe prlmlLlves ln Lhe famllv of
ob[ecLs bv uslna whaL are called wrapper c|asses

1here ls a wrapper class for everv prlmlLlve daLe Lvpe ln !ava 1hls class encapsulaLes a slnale value for
Lhe prlmlLlve daLa Lvpe lor lnsLance Lhe wrapper class for lnL ls lnLeaer for floaL ls lloaL and so on
8emember LhaL Lhe prlmlLlve name ls slmplv Lhe lowercase name of Lhe wrapper excepL for char whlch
maps Lo CharacLer and lnL whlch maps Lo lnLeaer

1he wrapper c|asses |n the Iava AI serve two pr|mary purposes
O 1o provlde a mechanlsm Lo wrap" prlmlLlve values ln an ob[ecL so LhaL Lhe prlmlLlves can be
lncluded ln acLlvlLles reserved for ob[ecLs llke as belna added Lo CollecLlons or reLurned from a
meLhod wlLh an ob[ecL reLurn value
O 1o provlde an assorLmenL of uLlllLv funcLlons for prlmlLlves MosL of Lhese funcLlons are relaLed
Lo varlous converslons converLlna prlmlLlves Lo and from SLrlna ob[ecLs and converLlna
prlmlLlves and SLrlna ob[ecLs Lo and from dlfferenL bases (or radlx) such as blnarv ocLal and
hexadeclmal
1he wrapper ob[ecL of a wrapper class can be creaLed ln one of Lwo wavs bv lnsLanLlaLlna Lhe wrapper
class wlLh Lhe new operaLor or bv lnvoklna a sLaLlc meLhod on Lhe wrapper class
Crating Wrappr Ubjcts witb tb nw Uprator


8efore we can lnsLanLlaLe a wrapper class we need Lo know lLs name and Lhe araumenLs lLs consLrucLor
accepLs 1he name of Lhe wrapper class correspondlna Lo each prlmlLlve daLa Lvpe alona wlLh Lhe
araumenLs lLs consLrucLor accepLs ls llsLed below

rlmlLlve daLaLvpeWrapper ClassConsLrucLor araumenLs
O boolean 8oolean boolean or SLrlna
O bvLe 8vLe bvLe or SLrlna
O char CharacLer char
O shorL ShorL shorL or SLrlna
O lnLlnLeaer lnL or SLrlna
O lona Lona lona or SLrlna
O floaLlloaL floaL double or SLrlna
O doubleuouble double or SLrlna
All Lhe wrapper classes are declared flnal 1haL means vou cannoL derlve a subclass from anv of LhemAll
Lhe wrapper classes excepL 8oolean and CharacLer are subclasses of an absLracL class called number
whereas 8oolean and CharacLer are derlved dlrecLlv from Lhe Cb[ecL classAll of Lhe wrapper classes
excepL CharacLer provlde Lwo consLrucLors one LhaL Lakes a prlmlLlve of Lhe Lvpe belna consLrucLed and
one LhaL Lakes a SLrlna represenLaLlon of Lhe Lvpe belna consLrucLedfor example
Code
oolean wboo = new oolean("false");
oolean yboo=new oolean(false);
yte wbyte = new yte("2");
yte ybyte=new yte(2);
Short wshort = new Short("4");
Short yshort = new Short(4);
Integer wint = new Integer("16");
Integer yint = new Integer(16);
Long wlong = new Long("123");
Long ylong = new Long(123);
Float wfloat = new Float("12.34f");
Float yfloat = new Float(12.34f);
Double wdouble = new Double("12.56d");
Double wdouble = new Double(12.56d);
Character c1 = new Character('c');
1he value mav also be passed as a varlable as shown ln Lhe followlna example
Code
boolean boo = false;
oolean wboo = new oolean(boo);
byte b = 2;
yte wbyte = new yte(b);
short s = 4;
Short wshort = new Short(s);
int i = 16;
Integer wint = new Integer(i);
long l = 123;
Long wlong = new Long(l);
float f = 12.34f;
Float wfloat = new Float(f);
double d = 12.56d;
Double wdouble = new Double(d);
noLe LhaL Lhere ls no wav Lo modlfv a wrapped valueLhaL ls Lhe wrapped values are lmmuLable 1o
wrap anoLher value vou need Lo creaLe anoLher ob[ecL
Wrapping Primitivs Using a static Mtbod


All wrapper classes offers sLaLlc valueCf() meLhods whlch alve vou anoLher approach Lo creaLlna
wrapper ob[ecLs 8ecause lLs a sLaLlc meLhod lL can be lnvoked dlrecLlv on Lhe class (wlLhouL
lnsLanLlaLlna lL) and wlll reLurn Lhe correspondlna ob[ecL LhaL ls wrapplna whaL vou passed ln as an
araumenL

8oLh meLhods Lake a SLrlna represenLaLlon of Lhe approprlaLe Lvpe of prlmlLlve as Lhelr flrsL araumenL
Lhe second meLhod (when provlded) Lakes an addlLlonal araumenL lnL radlx whlch lndlcaLes ln whaL
base (for example blnarv ocLal or hexadeclmal) Lhe flrsL araumenL ls represenLedfor example
Code
Integer i2 = Integer.valueJf("101011", 2); // converts 101011 to 43 and
assigns the
// value 43 to the Integer object
i2
or
Code
Float f2 = Float.valueJf("3.14f"); // assigns 3.14 to the Float object f2
,ethods to Create Wrapper Cb[ects

Wrapper classmeLhod SlanaLuremeLhod araumenLs
O 8oolean sLaLlc 8oolean valueCf()boolean or SLrlna
O CharacLer sLaLlc CharacLer valueCf()Char
O 8vLe sLaLlc 8vLe valueCf()bvLe SLrlna or SLrlna and radlx
O ShorL sLaLlc ShorL valueCf()shorL SLrlna or SLrlna and radlx
O lnLeaer sLaLlc lnLeaer valueCf()lnL SLrlna or SLrlna and radlx
O Lona sLaLlc Lona valueCf()lona SLrlna or SLrlna and radlx
O lloaL sLaLlc lloaL valueCf()floaL or SLrlna
O uouble sLaLlc uouble valueCf()double or SLrlna
1he valueCf() meLhod ln Lhe CharacLer class accepLs onlv char as an araumenL whlle anv oLher
wrapper class wlll accepL elLher Lhe correspondlna prlmlLlve Lvpe or SLrlna as an araumenL1he
valueCf() meLhod ln Lhe lnLeaer number wrapper classes (8vLe ShorL lnLeaer and Lona) also accepLs
Lwo araumenLs LoaeLher a SLrlna and a radlx where radlx ls Lhe base
Using Wrappr Convrsion Utilitis


A sLoraae capablllLv wlLhouL Lhe correspondlna reLrleval capablllLv ls noL of much use Cnce vou sLore a
prlmlLlve ln a wrapper ob[ecL ofLen vou wlll wanL Lo reLrleve Lhe sLored prlmlLlve aL a laLer Llme

Ia|ue() method

When vou need Lo converL Lhe value of a wrapped numerlc Lo a prlmlLlve use one of Lhe manv
xxxvalue() meLhods All of Lhe meLhods ln Lhls famllv are noara meLhods Lach of Lhe slx numerlc
wrapper classes has slx meLhods so LhaL anv numerlc wrapper can be converLed Lo anv prlmlLlve
numerlc Lvpe
Code
Integer i2 = new Integer(42); // make a new wrapper object
byte b = i2.byteValue(); // convert i2's value to a byte primitive
short s = i2.shortValue(); // another of Integer's xxxValue methods
double d = i2.doubleValue(); // yet another of Integer's xxxValue methods
or
Code
Float f2 = new Float(3.14f); // make a new wrapper object
short s = f2.shortValue(); // convert f2's value to a short primitive
System.out.println(s); // result is 3 (truncated, not rounded)
arse(5tr|ng) method

lf vou do noL need Lo sLore a value ln a wrapper buL [usL wanL Lo perform a qulck operaLlon on lL such as
converLlna Lhe Lvpe vou can do lL bv uslna an approprlaLe sLaLlc meLhod of Lhe approprlaLe wrapper
class lor example all Lhe wrapper classes excepL CharacLer offer a sLaLlc meLhod LhaL has Lhe followlna
slanaLure

static <type parse<Type(String s)

1he Lvpe mav be anv of Lhe prlmlLlve Lvpes excepL char (bvLe shorL lnL lona floaL double or
boolean) and Lhe 1vpe ls Lhe same as Lvpe wlLh Lhe flrsL leLLer uppercased for example

static int parseInt (String s)

Lach of Lhese meLhods parses Lhe sLrlna passed ln as a parameLer and reLurns Lhe correspondlna
prlmlLlve Lvpe

lor example conslder Lhe followlna code
Code
String s = "123";
int i = Integer.parseInt(s);
1he second llne wlll asslan an lnL value of 123 Lo Lhe lnL varlable l

,ethods to Convert 5tr|ngs to r|m|t|ve 1ypes

Wrapper Class MeLhod SlanaLure MeLhod AraumenLs
O 8oolean sLaLlc boolean parse8oolean() SLrlna
O CharacLer noL Avallable
O 8vLe sLaLlc bvLe parse8vLe() SLrlna or SLrlna and radlx
O ShorL sLaLlc shorL parseShorL() SLrlna or SLrlna and radlx
O lnLeaer sLaLlc lnL parselnL() SLrlna or SLrlna and radlx
O Lona sLaLlc lona parseLona() SLrlna or SLrlna and radlx
O lloaL sLaLlc floaL parselloaL() SLrlna
O uouble sLaLlc double parseuouble() double or SLrlna
parsek() and va|ueCf()

1he slx parsexxx() meLhods (one for each numerlc wrapper Lvpe) are closelv relaLed Lo Lhe valueCf()
meLhod LhaL exlsLs ln all of Lhe numerlc wrapper classes (plus 8oolean) 8oLh parsexxx() and valueCf()
Lake a SLrlna as an araumenL Lhrow a numberlormaLLxcepLlon lf Lhe SLrlna araumenL ls noL properlv
formed and can converL SLrlna ob[ecLs from dlfferenL bases (radlx) when Lhe underlvlna prlmlLlve Lvpe
ls anv of Lhe four lnLeaer Lvpes

1he dlfference beLween Lhe Lwo meLhods ls
O parsexxx() reLurns Lhe named prlmlLlve
O valueCf() reLurns a newlv creaLed wrapped ob[ecL of Lhe Lvpe LhaL lnvoked Lhe meLhod
Some examples of Lhese meLhods ln acLlon
Code
double d4 = Double.parseDouble("3.14"); // convert a String to a
primitive
System.out.println("d4 = " + d4); // result is "d4 = 3.14"
Double d5 = Double.valueJf("3.14"); // create a Double object
System.out.println(d5 instanceof Double ); // result is "true"
1he nexL examples lnvolve uslna Lhe radlx araumenL (ln Lhls case blnarv)
Code
long L2 = Long.parseLong("101010", 2); // binary String to a primitive
System.out.println("L2 = " + L2); // result is "L2 = 42"
Long L3 = Long.valueJf("101010", 2); // binary String to Long object
System.out.println("L3 value = " + L3); // result is "L2 value = 42"
to5tr|ng() method

1he class Cb[ecL Lhe super class of all classes has a LoSLrlna() meLhod Slnce we know LhaL all oLher !ava
classes lnherlL from class Cb[ecL we also know LhaL all oLher !ava classes have a LoSLrlna() meLhod 1he
ldea of Lhe LoSLrlna() meLhod ls Lo allow vou Lo aeL some meanlnaful represenLaLlon of a alven ob[ecL
lor lnsLance lf vou have a CollecLlon of varlous Lvpes of ob[ecLs vou can loop Lhrouah Lhe CollecLlon and
prlnL ouL some sorL of meanlnaful represenLaLlon of each ob[ecL uslna Lhe LoSLrlna() meLhod whlch ls
auaranLeed Lo be ln everv class All of Lhe wrapper classes have a noara nonsLaLlc lnsLance verslon of
LoSLrlna() 1hls meLhod reLurns a SLrlna wlLh Lhe value of Lhe prlmlLlve wrapped ln Lhe ob[ecLfor
lnsLance
Code
Double d = new Double("3.14");
System.out.println("d = " + d.toString() ); // result is "d = 3.14"
All of Lhe numerlc wrapper classes provlde an overloaded sLaLlc LoSLrlna() meLhod LhaL Lakes a prlmlLlve
numerlc of Lhe approprlaLe Lvpe (uoubleLoSLrlna() Lakes a double LonaLoSLrlna() Lakes a lona eLc)
and of course reLurns a SLrlna wlLh LhaL prlmlLlve's valuefor example
Code
System.out.println("d = " + Double.toString(3.14); // result is "d = 3.14"
llnallv lnLeaer and Lona provlde a Lhlrd LoSLrlna() meLhod lL ls sLaLlc lLs flrsL araumenL ls Lhe
approprlaLe prlmlLlve and lLs second araumenL ls a radlx 1he radlx araumenL Lells Lhe meLhod Lo Lake
Lhe flrsL araumenL (whlch ls radlx 10 or base 10 bv defaulL) and converL lL Lo Lhe radlx provlded Lhen
reLurn Lhe resulL as a SLrlnafor lnsLance
Code
System.out.println("hex = " + Long.toString(254,16); // result is "hex = fe"
tok5tr|ng() method(8|nary neadec|ma| Ccta|)

1he lnLeaer and Lona wrapper classes leL vou converL numbers ln base 10 Lo oLher bases 1hese
converslon meLhods LoxxxSLrlna() Lake an lnL or lona and reLurn a SLrlna represenLaLlon of Lhe
converLed number for example
Code
String s3 = Integer.toHexString(254); // convert 254 to hex
System.out.println("254 in hex = " + s3); // result is "254 in hex = fe"
String s4 = Long.toJctalString(254); // convert 254 to octal
System.out.println("254 in octal = "+ s4); // result is "254 in octal = 376"

Você também pode gostar