UML Notation for Classes gra phics/01fig01.gif Declaring Members: Fields and Methods Ex a m ple 1.1 sho ws the de finitio n o f the cla ss C harStack de picte d in Figure 1.1.. priv ate
Trang 1By Kha lid A Mugha l, R o lf W R a sm usse n
P ublishe r: Addiso n W e sle y
A Programmer's Guide to Java(TM) Certification, Second Edition, co nta ins de ta ile d co ve ra ge o f a ll e x a m to pics a nd o bje ctive s, he lpful co de e x a m ple s, e x e rcise s, re vie w
que stio ns, a nd se ve ra l full pra ctice e x a m s In a dditio n, a s a co m pre he nsive prim e r to the Ja va pro gra m m ing la ngua ge , this bo o k is a n inva lua ble re fe re nce to o l fo rthe re a de r
T his ne w e ditio n ha s be e n tho ro ughly upda te d to fo cus o n the la te st ve rsio n o f the e x a m (C X-310-035) In pa rticula r, it co nta ins m o re in-de pth e x pla na tio ns o f thesynta x a nd usa ge o f co re la ngua ge fe a ture s tha t a re no w e m pha size d o n the e x a m T he a cco m pa nying C D co nta ins a ve rsio n o f the SC P J2 1.4 Ex a m Sim ula to r
de ve lo pe d by W hizla bs So ftwa re , which include s se ve ra l m o ck e x a m s a nd sim ula te s the e x a m -ta k ing e x pe rie nce T he C D a lso co nta ins the co m ple te so urce co de
fo r a ll the e x a m ple s, a nd so lutio ns to the pro gra m m ing e x e rcise s
W ha t yo u will find in this bo o k :
Ex te nsive co ve ra ge o f a ll the o bje ctive s de fine d fo r the Sun C e rtifie d P ro gra m m e r fo r Ja va 2 P la tfo rm 1.4 e x a m
Ea sy-to -fo llo w structure with cha pte rs o rga nize d a cco rding to the e x a m o bje ctive s a s la id o ut by Sun Micro syste m s
Sum m a rie s tha t cle a rly sta te a nd diffe re ntia te the e x a m o bje ctive s a nd the supple m e nta ry o bje ctive s to be co ve re d in e a ch cha pte r
A list o f Sun's o bje ctive s fo r the SC P J2 1.4 e x a m , a nd a guide to ta k ing the e x a m
A co m ple te m o ck e x a m with ne w que stio ns (no t re pe a ts o f re vie w que stio ns)
Num e ro us e x a m -re le va nt re vie w que stio ns to te st yo ur unde rsta nding o f e a ch m a jo r to pic, with a nno ta te d a nswe rs
P ro gra m m ing e x e rcise s a nd so lutio ns a t the e nd o f e a ch cha pte r
C o pio us co de e x a m ple s illustra ting co nce pts whe re the co de ha s be e n co m pile d a nd tho ro ughly te ste d o n m ultiple pla tfo rm s
P ro gra m o utput de m o nstra ting e x pe cte d re sults fro m running the e x a m ple s
Ex te nsive use o f UML (Unifie d Mo de ling La ngua ge ) fo r illustra tio n purpo se s
An intro ductio n to ba sic te rm ino lo gy a nd co nce pts in o bje ct-o rie nte d pro gra m m ing
Advice o n ho w to a vo id co m m o n pitfa lls in m a ste ring the la ngua ge a nd ta k ing the e x a m
P la tfo rm -inde pe nde nt co ve ra ge pla tfo rm -spe cific de ta ils a re pro vide d whe re re le va nt
Info rm a tio n a bo ut the SC P J2 Upgra de e x a m
[ Team LiB ]
Trang 2By Kha lid A Mugha l, R o lf W R a sm usse n
P ublishe r: Addiso n W e sle y
Abo ut the Autho rs
Ack no wle dgm e nts (First Editio n)
Ack no wle dgm e nts (Se co nd Editio n)
C ha pte r 1 Ba sics o f Ja va P ro gra m m ing
Se ctio n 1.1 Intro ductio n
Se ctio n 1.2 C la sse s
Se ctio n 1.3 O bje cts
Se ctio n 1.4 Insta nce Me m be rs
Se ctio n 1.5 Sta tic Me m be rs
Se ctio n 1.6 Inhe rita nce
Se ctio n 1.7 Aggre ga tio n
P ro gra m m ing Ex e rcise s
C ha pte r 2 La ngua ge Funda m e nta ls
Se ctio n 2.1 Ba sic La ngua ge Ele m e nts
R e vie w Q ue stio ns
Se ctio n 2.2 P rim itive Da ta T ype s
R e vie w Q ue stio ns
Se ctio n 2.3 Va ria ble De cla ra tio ns
Se ctio n 2.4 Initia l Va lue s fo r Va ria ble s
Se ctio n 3.1 P re ce de nce a nd Asso cia tivity R ule s fo r O pe ra to rs
Se ctio n 3.2 Eva lua tio n O rde r o f O pe ra nds
Trang 3Se ctio n 3.4 Sim ple Assignm e nt O pe ra to r =
R e vie w Q ue stio ns
Se ctio n 3.5 Arithm e tic O pe ra to rs: *, /, %, ,
R e vie w Q ue stio ns
Se ctio n 3.6 T he Bina ry String C o nca te na tio n O pe ra to r +
Se ctio n 3.7 Va ria ble Incre m e nt a nd De cre m e nt O pe ra to rs: ++,
Se ctio n 3.18 P a ssing P rim itive Da ta Va lue s
Se ctio n 3.19 P a ssing O bje ct R e fe re nce Va lue s
Se ctio n 3.20 P a ssing Arra y R e fe re nce s
Se ctio n 3.21 Arra y Ele m e nts a s Actua l P a ra m e te rs
Se ctio n 3.22 final P a ra m e te rs
Se ctio n 3.23 P ro gra m Argum e nts
R e vie w Q ue stio ns
C ha pte r Sum m a ry
P ro gra m m ing Ex e rcise s
C ha pte r 4 De cla ra tio ns a nd Acce ss C o ntro l
Se ctio n 4.1 Arra ys
R e vie w Q ue stio ns
Se ctio n 4.2 De fining C la sse s
Se ctio n 4.3 De fining Me tho ds
Se ctio n 4.4 C o nstructo rs
Se ctio n 4.5 Sco pe R ule s
R e vie w Q ue stio ns
Se ctio n 4.6 P a ck a ge s
Se ctio n 4.7 Acce ssibility Mo difie rs fo r T o p-le ve l C la sse s a nd Inte rfa ce s
Se ctio n 4.8 O the r Mo difie rs fo r C la sse s
P ro gra m m ing Ex e rcise s
C ha pte r 5 C o ntro l Flo w, Ex ce ptio n Ha ndling, a nd Asse rtio ns
Se ctio n 5.1 O ve rvie w o f C o ntro l Flo w Sta te m e nts
Se ctio n 5.2 Se le ctio n Sta te m e nts
R e vie w Q ue stio ns
Se ctio n 5.3 Ite ra tio n Sta te m e nts
Se ctio n 5.4 T ra nsfe r Sta te m e nts
R e vie w Q ue stio ns
Se ctio n 5.5 Sta ck -ba se d Ex e cutio n a nd Ex ce ptio n P ro pa ga tio n
Se ctio n 5.6 Ex ce ptio n T ype s
Se ctio n 5.7 Ex ce ptio n Ha ndling: try, catch, a nd finally
Se ctio n 5.8 throw Sta te m e nt
Se ctio n 5.9 throws C la use
R e vie w Q ue stio ns
Se ctio n 5.10 Asse rtio ns
R e vie w Q ue stio ns
C ha pte r Sum m a ry
P ro gra m m ing Ex e rcise s
C ha pte r 6 O bje ct-o rie nte d P ro gra m m ing
Se ctio n 6.1 Single Im ple m e nta tio n Inhe rita nce
Se ctio n 6.5 C o m ple ting the T ype Hie ra rchy
Se ctio n 6.6 Assigning, P a ssing, a nd C a sting R e fe re nce Va lue s
Trang 4Se ctio n 6.7 P o lym o rphism a nd Dyna m ic Me tho d Lo o k up
R e vie w Q ue stio ns
Se ctio n 6.8 Inhe rita nce vs Aggre ga tio n
R e vie w Q ue stio ns
C ha pte r Sum m a ry
P ro gra m m ing Ex e rcise s
C ha pte r 7 Ne ste d C la sse s And Inte rfa ce s
Se ctio n 7.1 O ve rvie w o f Ne ste d C la sse s a nd Inte rfa ce s
Se ctio n 7.2 Sta tic Me m be r C la sse s a nd Inte rfa ce s
Se ctio n 7.3 No n-sta tic Me m be r C la sse s
P ro gra m m ing Ex e rcise
C ha pte r 8 O bje ct Life tim e
Se ctio n 8.1 Ga rba ge C o lle ctio n
Se ctio n 9.1 Multita sk ing
Se ctio n 9.2 O ve rvie w o f T hre a ds
Se ctio n 9.3 T hre a d C re a tio n
P ro gra m m ing Ex e rcise s
C ha pte r 10 Funda m e nta l C la sse s
Se ctio n 10.1 O ve rvie w o f the jav a.lang P a ck a ge
P ro gra m m ing Ex e rcise s
C ha pte r 11 C o lle ctio ns a nd Ma ps
Se ctio n 11.1 T he C o lle ctio ns Fra m e wo rk
Se ctio n 11.2 C o lle ctio ns
Se ctio n 11.7 Im ple m e nting the equals(), hashC ode(), a nd compareTo() Me tho ds
Se ctio n 11.8 W o rk ing with C o lle ctio ns
R e vie w Q ue stio ns
C ha pte r Sum m a ry
P ro gra m m ing Ex e rcise s
Appe ndix A T a k ing the SC P J2 1.4 Ex a m
Se ctio n A.1 P re pa ring fo r the P ro gra m m e r Ex a m
Se ctio n A.2 R e giste ring fo r the Ex a m
Se ctio n A.3 Ho w the Ex a m ina tio n Is C o nducte d
Se ctio n A.4 T he Q ue stio ns
Se ctio n A.5 Mo ving o n to the De ve lo pe r Ex a m
Appe ndix B O bje ctive s fo r the SC P J2 1.4 Ex a m
Se ctio n 1: De cla ra tio ns a nd Acce ss C o ntro l (C ha pte r 4)
Se ctio n 2: Flo w C o ntro l, Asse rtio ns, a nd Ex ce ptio n Ha ndling (C ha pte r 5)
Se ctio n 3: Ga rba ge C o lle ctio n (C ha pte r 8)
Se ctio n 4: La ngua ge Funda m e nta ls (C ha pte r 2)
Se ctio n 5: O pe ra to rs a nd Assignm e nts (C ha pte r 3)
Trang 5Se ctio n 7: T hre a ds (C ha pte r 9)
Se ctio n 8: Funda m e nta l C la sse s in the ja va la ng P a ck a ge (C ha pte r 10)
Se ctio n 9: T he C o lle ctio ns Fra m e wo rk (C ha pte r 11)
Appe ndix C O bje ctive s fo r the Ja va 2 P la tfo rm Upgra de Ex a m
Se ctio n 1: De cla ra tio ns a nd Acce ss C o ntro l
Se ctio n 2: Flo w C o ntro l, Asse rtio ns, a nd Ex ce ptio n Ha ndling
Se ctio n 3: Ga rba ge C o lle ctio n
Se ctio n 4: La ngua ge Funda m e nta ls
Se ctio n 5: O pe ra to rs a nd Assignm e nts
Se ctio n 6: O ve rlo a ding, O ve rriding, R untim e T ype a nd O bje ct O rie nta tio n
Se ctio n 7: T hre a ds
Se ctio n 8: T he C o lle ctio ns Fra m e wo rk
Appe ndix D Anno ta te d Answe rs to R e vie w Q ue stio ns
Se ctio n 1 Ba sics o f Ja va P ro gra m m ing
Se ctio n 2 La ngua ge Funda m e nta ls
Se ctio n 3 O pe ra to rs a nd Assignm e nts
Se ctio n 4 De cla ra tio ns a nd Acce ss C o ntro l
Se ctio n 5 C o ntro l Flo w, Ex ce ptio n Ha ndling, a nd Asse rtio ns
Se ctio n 6 O bje ct-o rie nte d P ro gra m m ing
Se ctio n 7 Ne ste d C la sse s a nd Inte rfa ce s
Se ctio n 8 O bje ct Life tim e
Se ctio n 9 T hre a ds
Se ctio n 10 Funda m e nta l C la sse s
Se ctio n 11 C o lle ctio ns a nd Ma ps
Appe ndix E So lutio ns to P ro gra m m ing Ex e rcise s
Se ctio n 1 Ba sics o f Ja va P ro gra m m ing
Se ctio n 2 La ngua ge Funda m e nta ls
Se ctio n 3 O pe ra to rs a nd Assignm e nts
Se ctio n 4 De cla ra tio ns a nd Acce ss C o ntro l
Se ctio n 5 C o ntro l Flo w, Ex ce ptio n Ha ndling, a nd Asse rtio ns
Se ctio n 6 O bje ct-o rie nte d P ro gra m m ing
Se ctio n 7 Ne ste d C la sse s a nd Inte rfa ce s
Se ctio n 8 Ba sics o f Ja va P ro gra m m ing
Se ctio n 9 T hre a ds
Se ctio n 10 Funda m e nta l C la sse s
Se ctio n 11 C o lle ctio ns a nd Ma ps
Appe ndix F Mo ck Ex a m
Answe rs to Q ue stio ns
Appe ndix G Num be r Syste m s a nd Num be r R e pre se nta tio n
Se ctio n G.1 Num be r Syste m s
Se ctio n G.2 R e la tio nship be twe e n Bina ry, O cta l, a nd He x a de cim a l Num be rs
Se ctio n G.3 C o nve rting De cim a ls
Se ctio n G.4 R e pre se nting Inte ge rs
Appe ndix H Abo ut the C D
Se ctio n H.1 W hizla bs Ex a m Sim ula to rs
Se ctio n H.2 Ite m s fro m the Bo o k
Single -Use r Lice nse Agre e m e nt
Im po rta nt T e rm s o f Agre e m e nt
T e nure o f the Lice nse Agre e m e nt
Lim ite d W a rra nty Unde r the Lice nse
R e m e die s P ro vide d Unde r the Lice nse
Lia bilitie s Unde r the Lice nse
O the r Lim ita tio ns Unde r the Lice nse
Ge ne ra l No te
Inde x
[ Team LiB ]
Trang 6Ma ny o f the de signa tio ns use d by m a nufa cture rs a nd se lle rs to distinguish the ir pro ducts a re cla im e d a s tra de m a rk s W he re tho se de signa tio ns a ppe a r in this
bo o k , a nd Addiso n-W e sle y wa s a wa re o f a tra de m a rk cla im , the de signa tio ns ha ve be e n printe d with initia l ca pita l le tte rs o r in a ll ca pita ls
T he a utho rs a nd publishe r ha ve ta k e n ca re in the pre pa ra tio n o f this bo o k , but m a k e no e x pre sse d o r im plie d wa rra nty o f a ny k ind a nd a ssum e no re spo nsibility
fo r e rro rs o r o m issio ns No lia bility is a ssum e d fo r incide nta l o r co nse que ntia l da m a ge s in co nne ctio n with o r a rising o ut o f the use o f the info rm a tio n o r pro gra m s
co nta ine d he re in
T he publishe r o ffe rs disco unts o n this bo o k whe n o rde re d in qua ntity fo r bulk purcha se s a nd spe cia l sa le s Fo r m o re info rm a tio n, ple a se co nta ct:
U.S C o rpo ra te a nd Go ve rnm e nt Sa le s
(800) 382-3419
co rpsa le s@pe a rso nte chgro up.co m
Fo r sa le s o utside o f the U.S., ple a se co nta ct:
Inte rna tio na l Sa le s
(317) 581-3793
inte rna tio na l@pe a rso nte chgro up.co m
Visit Addiso n-W e sle y o n the W e b: www.a wpro fe ssio na l.co m
Library of Congress Cataloging-in-Publication Data is available.
C o pyright © 2004 by P e a rso n Educa tio n, Inc
All rights re se rve d No pa rt o f this publica tio n m a y be re pro duce d, sto re d in a re trie va l syste m , o r tra nsm itte d, in a ny fo rm , o r by a ny m e a ns, e le ctro nic,
m e cha nica l, pho to co pying, re co rding, o r o the rwise , witho ut the prio r co nse nt o f the publishe r P rinte d in the Unite d Sta te s o f Am e rica P ublishe d sim ulta ne o usly in
C a na da
Fo r info rm a tio n o n o bta ining pe rm issio n fo r use o f m a te ria l fro m this wo rk , ple a se subm it a writte n re que st to :
P e a rso n Educa tio n, Inc
Trang 7[ Team LiB ]
List of Figures
Chapter 1
1.1 UML No ta tio n fo r C la sse s
1.2 UML No ta tio n fo r O bje cts
1.3 Alia se s
1.4 C la ss Dia gra m Sho wing Sta tic Me m be rs o f a C la ss
1.5 Me m be rs o f a C la ss
1.6 C la ss Dia gra m De picting Inhe rita nce R e la tio n
1.7 C la ss Dia gra m De picting Aggre ga tio n
Chapter 2
2.1 P rim itive Da ta T ype s in Ja va
2.2 Ja va So urce File Structure
Chapter 3
3.1 W ide ning Num e ric C o nve rsio ns
3.2 O ve rflo w a nd Unde rflo w in Flo a ting-po int Arithm e tic
3.3 Num e ric P ro m o tio n in Arithm e tic Ex pre ssio ns
3.4 P a ra m e te r P a ssing: P rim itive Da ta Va lue s
3.5 P a ra m e te r P a ssing: O bje ct R e fe re nce Va lue s
4.4 P ublic Acce ssibility
4.5 P ro te cte d Acce ssibility
4.6 De fa ult Acce ssibility
4.7 P riva te Acce ssibility
Chapter 5
5.1 Activity Dia gra m fo r if Sta te m e nts
5.2 Activity Dia gra m fo r switch Sta te m e nt
5.3 Activity Dia gra m fo r while Sta te m e nt
5.4 Activity Dia gra m fo r do-while Sta te m e nt
5.5 Activity Dia gra m fo r the for Sta te m e nt
5.6 Me tho d Ex e cutio n
5.7 Ex ce ptio n P ro pa ga tio n
5.8 P a rtia l Ex ce ptio n Inhe rita nce Hie ra rchy
5.9 try -catch-finally Blo ck
5.10 Ex ce ptio n Ha ndling (Sce na rio 1)
5.11 Ex ce ptio n Ha ndling (Sce na rio 2)
5.12 Ex ce ptio n Ha ndling (Sce na rio 3)
5.13 Ex e cutio n o f the Sim ple assert Sta te m e nt (W he n Asse rtio ns Are Ena ble d)
Chapter 6
6.1 Inhe rita nce Hie ra rchy
6.2 Inhe rita nce R e la tio nship be twe e n String a nd O bject C la sse s
6.3 Inhe rita nce R e la tio ns
6.4 R e fe re nce T ype Hie ra rchy
Trang 86.6 Im ple m e nting Da ta Structure s by Inhe rita nce a nd Aggre ga tio n
Chapter 7
7.1 Sta tic Me m be r C la sse s a nd Inte rfa ce s
7.2 O ute r O bje ct with Asso cia te d Inne r O bje cts
Chapter 8
8.1 Me m o ry O rga niza tio n a t R untim e
Chapter 9
9.1 Spa wning T hre a ds Using a Runnable O bje ct
9.2 Spa wning T hre a ds—Ex te nding the Thread C la ss
10.1 P a rtia l Inhe rita nce Hie ra rchy in the jav a.lang P a ck a ge
10.2 C o nve rting Va lue s be twe e n P rim itive , W ra ppe r, a nd String T ype s
Chapter 11
11.1 T he C o re Inte rfa ce s
11.2 T he C o re C o lle ctio n Inte rfa ce s a nd T he ir Im ple m e nta tio ns
11.3 T he C o re Ma p Inte rfa ce s a nd T he ir Im ple m e nta tio ns
11.4 Bulk O pe ra tio ns o n C o lle ctio ns
G.1 C o nve rting be twe e n Bina ry, O cta l, a nd He x a de cim a l
[ Team LiB ]
Trang 92.5 Ex a m ple s o f De cim a l, O cta l, a nd He x a de cim a l Lite ra ls
2.6 Ex a m ple s o f Unico de Va lue s
2.7 Esca pe Se que nce s
2.8 Ex a m ple s o f Esca pe Se que nce \ddd
2.9 R a nge o f Inte ge r Va lue s
2.10 R a nge o f C ha ra cte r Va lue s
2.11 R a nge o f Flo a ting-po int Va lue s
3.4 Ex a m ple s o f Arithm e tic Ex pre ssio n Eva lua tio n
3.5 Arithm e tic C o m po und Assignm e nt O pe ra to rs
3.6 R e la tio na l O pe ra to rs
3.7 P rim itive Da ta Va lue Equa lity O pe ra to rs
3.8 R e fe re nce Equa lity O pe ra to rs
3.9 Bo o le a n Lo gica l O pe ra to rs
3.10 T ruth-va lue s fo r Bo o le a n Lo gica l O pe ra to rs
3.11 Bo o le a n Lo gica l C o m po und Assignm e nt O pe ra to rs
3.12 C o nditio na l O pe ra to rs
3.13 T ruth-va lue s fo r C o nditio na l O pe ra to rs
3.14 Inte ge r Bitwise O pe ra to rs
3.15 R e sult T a ble fo r Bitwise O pe ra to rs
3.16 Ex a m ple s o f Bitwise O pe ra tio ns
3.17 Bitwise C o m po und Assignm e nt O pe ra to rs
3.18 Shift O pe ra to rs
3.19 Shift C o m po und Assignm e nt O pe ra to rs
3.20 P a ra m e te r P a ssing
Chapter 4
4.1 Acce ssing Me m be rs within a C la ss
4.2 Sum m a ry o f Acce ssibility Mo difie rs fo r C la sse s a nd Inte rfa ce s
4.3 Sum m a ry o f O the r Mo difie rs fo r C la sse s a nd Inte rfa ce s
4.4 Sum m a ry o f Acce ssibility Mo difie rs fo r Me m be rs
4.5 Sum m a ry o f O the r Mo difie rs fo r Me m be rs
Chapter 5
Trang 105.2 Gra nula ritie s fo r Ena bling a nd Disa bling Asse rtio ns a t R untim e
5.3 Ena bling a nd Disa bling Asse rtio ns in All Syste m C la sse s a t R untim e
11.1 C o re Inte rfa ce s in the C o lle ctio ns Fra m e wo rk
11.2 Sum m a ry o f C o lle ctio n a nd Ma p Im ple m e nta tio ns
11.3 Bulk O pe ra tio ns a nd Se t Lo gic
G.1 Num be r Syste m s
G.2 R e pre se nting Signe d by te Va lue s Using 2's C o m ple m e nt
[ Team LiB ]
Trang 11[ Team LiB ]
List of Examples
Chapter 1
1.1 Ba sic Ele m e nts o f a C la ss De finitio n
1.2 Sta tic Me m be rs in C la ss De finitio n
1.3 De fining a Subcla ss
1.4 An Applica tio n
Chapter 2
2.1 De fa ult Va lue s fo r Fie lds
2.2 Fla gging Uninitia lize d Lo ca l Va ria ble s o f P rim itive Da ta T ype s
2.3 Fla gging Uninitia lize d Lo ca l R e fe re nce Va ria ble s
Chapter 3
3.1 Num e ric P ro m o tio n in Arithm e tic Ex pre ssio ns
3.2 Bitwise O pe ra tio ns
3.3 P a ssing P rim itive Va lue s
3.4 P a ssing O bje ct R e fe re nce Va lue s
3.5 P a ssing Arra ys
3.6 Arra y Ele m e nts a s P rim itive Da ta Va lue s
3.7 Arra y Ele m e nts a s O bje ct R e fe re nce Va lue s
3.8 P a ssing P ro gra m Argum e nts
Chapter 4
4.1 Using Arra ys
4.2 Using Ano nym o us Arra ys
4.3 Using Multidim e nsio na l Arra ys
4.4 Using this R e fe re nce
4.5 Na m e spa ce s
4.6 C la ss Sco pe
4.7 Acce ssibility Mo difie rs fo r C la sse s a nd Inte rfa ce s
4.8 Abstra ct C la sse s
4.9 P ublic Acce ssibility o f Me m be rs
4.10 Acce ssing Sta tic Me m be rs
4.11 Acce ssing Fina l Me m be rs
4.12 Synchro nize d Me tho ds
Chapter 5
5.1 Fa ll T hro ugh in switch Sta te m e nt
5.2 Using bre a k in switch Sta te m e nt
5.3 Ne ste d switch Sta te m e nt
5.12 try -catch-finally C o nstruct
5.13 try -finally C o nstruct
Trang 125.15 T hro wing Ex ce ptio ns
5.16 throws C la use
5.17 Asse rtio ns
Chapter 6
6.1 Ex te nding C la sse s: Inhe rita nce a nd Acce ssibility
6.2 Illustra ting Inhe rita nce
6.3 O ve rriding, O ve rlo a ding, a nd Hiding
6.4 O ve rlo a de d Me tho d R e so lutio n
6.5 Using super Ke ywo rd
6.6 C o nstructo r O ve rlo a ding
6.7 this() C o nstructo r C a ll
6.8 super() C o nstructo r C a ll
6.9 Inte rfa ce s
6.10 Va ria ble s in Inte rfa ce s
6.11 Assigning a nd P a ssing R e fe re nce Va lue s
6.12 instanceof a nd C a st O pe ra to r
6.13 Using instanceof O pe ra to r
6.14 P o lym o rphism a nd Dyna m ic Me tho d Lo o k up
6.15 Im ple m e nting Da ta Structure s by Inhe rita nce a nd Aggre ga tio n
Chapter 7
7.1 O ve rvie w o f Ne ste d C la sse s a nd Inte rfa ce s
7.2 Sta tic Me m be r C la sse s a nd Inte rfa ce s
7.3 Im po rting Me m be r C la sse s
7.4 Acce ssing Me m be rs in Enclo sing C o nte x t
7.5 De fining No n-sta tic Me m be r C la sse s
7.6 Spe cia l Fo rm o f this a nd new C o nstructs in No n-sta tic Me m be r C la sse s
7.7 Inhe rita nce Hie ra rchy a nd Enclo sing C o nte x t
7.8 Ex te nding Inne r C la sse s
7.9 Acce ss in Enclo sing C o nte x t
7.10 Insta ntia ting Lo ca l C la sse s
7.11 De fining Ano nym o us C la sse s
7.12 Acce ssing De cla ra tio ns in Enclo sing C o nte x t
Chapter 8
8.1 Ga rba ge C o lle ctio n Eligibility
8.2 Using Fina lize rs
8.3 Invo k ing Ga rba ge C o lle ctio n
8.4 Initia lize r Ex pre ssio n O rde r a nd Me tho d C a lls
8.5 Ex ce ptio ns in Initia lize r Ex pre ssio ns
8.6 Sta tic Initia lize rs a nd Fo rwa rd R e fe re nce s
8.7 Sta tic Initia lize r Blo ck s a nd Ex ce ptio ns
8.8 Insta nce Initia lize rs a nd Fo rwa rd R e fe re nce s
8.9 Insta nce Initia lize r Blo ck in Ano nym o us C la ss
8.10 Ex ce ptio n Ha ndling in Insta nce Initia lize r Blo ck s
8.11 O bje ct Sta te C o nstructio n
8.12 Initia liza tio n unde r O bje ct Sta te C o nstructio n
Chapter 9
9.1 Im ple m e nting the Runnable Inte rfa ce
Trang 1310.1 Me tho ds in the O bject cla ss
10.2 String R e pre se nta tio n o f Inte ge rs
10.3 String C o nstructio n a nd Equa lity
10.4 R e a ding C ha ra cte rs fro m a String
11.5 Na tura l O rde r a nd T o ta l O rde r
11.6 Using So rte dMa ps
11.7 A Sim ple C la ss fo r Ve rsio n Num be r
11.8 Im plica tio ns o f No t O ve rriding the equals() Me tho d
11.9 Im ple m e nting the equals() Me tho d
11.10 Im plica tio ns o f O ve rriding the equals() Me tho d
11.11 Im ple m e nting the hashC ode() Me tho d
11.12 Im plica tio ns o f O ve rriding the hashC ode() Me tho d
11.13 Im ple m e nting the compareTo() Me tho d o f the C o m pa ra ble Inte rfa ce
11.14 Im plica tio ns o f Im ple m e nting the compareTo() Me tho d
[ Team LiB ]
Trang 14C o nside r the fo llo wing o bse rva tio ns:
So ftwa re co ntinue s to be co m e e ve r m o re pe rva sive , e ve r m o re ubiquito us in o ur live s
Inco m pe te nce se e m s to be the o nly thing we ca n co unt o n in to da y's wo rld a nd, e spe cia lly, in the do m a in o f so ftwa re
T he Ja va pro gra m m ing la ngua ge ha s be co m e a lingua fra nca fo r pro gra m m e rs a ll o ve r the wo rld
O ne ca n dra w va rie d co nclusio ns fro m the se co m m e nts O ne o f the m is tha t it is o f gre a t im po rta nce tha t pro gra m m e rs wo rk ing with the Ja va pro gra m m ing
la ngua ge sho uld be a s co m pe te nt a s po ssible
T he Ja va ce rtifica tio n pro gra m is a n im po rta nt e ffo rt a im e d a t pre cise ly this go a l P ra ctitio ne rs lo o k ing to o bta in such ce rtifica tio n ne e d go o d qua lity tra ining
m a te ria ls, which brings us to this bo o k
P ro gra m m ing is still m o re o f a n a rt tha n a scie nce , a nd will co ntinue to be so fo r the fo re se e a ble future Ma ste ring the intrica cie s o f a la rge a nd co m ple xpro gra m m ing la ngua ge is a cha lle nging ta sk tha t re quire s tim e a nd e ffo rt, a nd a bo ve a ll e x pe rie nce
R e a l pro gra m m ing re quire s m o re tha n just m a ste ry o f a pro gra m m ing la ngua ge It re quire s m a ste ry o f a co m puting pla tfo rm , with a rich se t o f libra rie s T he selibra rie s a re de signe d to sim plify the ta sk o f building re a listic a pplica tio ns, a nd the y do Aga in, the pra ctitio ne r is fa ce d with a da unting ta sk
T o a ddre ss the cle a r ne e d fo r pro fe ssio na l tra ining m a te ria l, a ple tho ra o f bo o k s ha ve be e n writte n purpo rting to tuto r pro gra m m e rs in the pro gra m m ing la ngua ge
a nd pla tfo rm sk ills the y re quire
T he cho ice is a s m ind bo ggling a s the m a te ria l within the bo o k s the m se lve s Sho uld o ne try Java for Frontally Lobotomized Simians o r Postmodern Java Dialectics? T he
re a de rship fo r the se bo o k s is la rge ly se lf se le cting I trust tha t if yo u, the re a de r, ha ve go tte n this fa r, yo u a re lo o k ing fo r so m e thing tha t is inte llige nt, ye tpra ctica l T his bo o k is o ne o f the fine st e ffo rts in this cro wde d a re na It brings a ne ce ssa ry le ve l o f a ca de m ic rigo r to a n a re a m uch in ne e d o f it, while re ta ining a n
e sse ntia lly pra gm a tic fla vo r
T he m a te ria l in this bo o k is pro ba bly a ll yo u ne e d to pa ss the Ja va ce rtifica tio n e x a m It ce rta inly isn't a ll yo u ne e d to be a go o d so ftwa re e ngine e r Y o u m ust
co ntinue le a rning a bo ut ne w te chno lo gie s T he ha rde st pa rt o f this is de a ling with things tha t a re co m ple te ly diffe re nt fro m wha t yo u a re fa m ilia r with Y e t this iswha t distinguishe s the to p flight e ngine e r fro m the m e dio cre o ne Ke e p a n o pe n m ind; it pa ys
Gila d Bra cha
Computational Theologist
Sun Java Software
http://ja va sun.co m /pe o ple /gbra cha /
[ Team LiB ]
Trang 15[ Team LiB ]
Preface
[ Team LiB ]
Trang 16Writing the Second Edition
T he e x a m fo r the Sun C e rtifie d P ro gra m m e r fo r Ja va 2 (SC P J2) P la tfo rm ha s cha nge d co nside ra bly since the first e ditio n o f this bo o k wa s publishe d T he m o st
no tice a ble cha nge in the curre nt ve rsio n o f the e x a m (SC P J2 1.4) is the re m o va l o f GUI a nd I/O , a nd the shifting o f e m pha sis to wa rd the co re fe a ture s o f the
la ngua ge In o ur o pinio n, the ne w e x a m de m a nds a n e ve n gre a te r unde rsta nding a nd a ctua l e x pe rie nce o f the la ngua ge , ra the r tha n m e re re cita tio n o f fa cts
P ro ficie ncy in the la ngua ge is the k e y to succe ss
T he I/O a nd GUI to pics o f the first e ditio n ha ve be e n e lim ina te d, a s the y ha ve no re le va nce fo r the SC P J2 e x a m T he se to pics a re m o re re le va nt fo r the Sun
C e rtifie d De ve lo pe r fo r Ja va 2 (SC DJ2) P la tfo rm e x a m a nd, the re fo re , re le ga te d to a po ssible future vo lum e o n the De ve lo pe r e x a m to pics
Since the e m pha sis o f the SC P J2 e x a m is o n the co re fe a ture s o f Ja va , the se co nd e ditio n pro vide s a n e ve n gre a te r in-de pth co ve ra ge o f the re le va nt to pics T he
bo o k co ve rs no t just the e x a m o bje ctive s, but a lso supple m e nta ry to pics tha t a id in m a ste ring the e x a m to pics
T he se co nd e ditio n is still a o ne -so urce guide fo r the SC P J2 e x a m : it pro vide s a m ix ture o f the o ry a nd pra ctice fo r the e x a m Use the bo o k to le a rn Ja va , pa ss the
SC P J2 e x a m , a nd a fte rwa rds, use it a s a ha ndy la ngua ge guide T he bo o k a lso ha s a n a ppe ndix de vo te d to the SC P J2 Upgra de e x a m T o fa cilita te pre pa ra tio n fo rthe e x a m s, the se co nd e ditio n co m e s with a C D co nta ining a dditio na l re so urce s, including se ve ra l m o ck e x a m s de ve lo pe d by W hizla bs So ftwa re
W e ha ve ta k e n into co nside ra tio n the fe e dba ck we ha ve re ce ive d fro m re a de rs T he m a ny ho urs spe nt in ha ndling the de luge o f e -m a il ha ve no t be e n in va in.Eve ry single e -m a il is a ppre cia te d a nd is he re by a ck no wle dge d
P re pa ring the se co nd e ditio n dispe lle d a ll o ur illusio ns a bo ut se co nd e ditio ns be ing, to put it co llo quia lly, a pie ce o f ca k e Eve ry se nte nce fro m the first e ditio n ha s
be e n we ighe d ca re fully, a nd no t m a ny pa ra gra phs ha ve e sca pe d re writing
UML (Unifie d Mo de ling La ngua ge ) is a lso e x te nsive ly e m plo ye d in this e ditio n Num e ro us ne w re vie w que stio ns ha ve be e n a dde d In co ve ring the ne w to pics a nd
e x pa nding the e x isting o ne s, ne w e x a m ple s, figure s, a nd ta ble s we re a lso spe cifica lly cre a te d fo r the se co nd e ditio n
[ Team LiB ]
Trang 17[ Team LiB ]
About This Book
T his bo o k pro vide s a n e x te nsive co ve ra ge o f the Ja va pro gra m m ing la ngua ge a nd its co re Applica tio n P ro gra m m ing Inte rfa ce s (AP Is), with pa rticula r e m pha sis o nits synta x a nd usa ge T he bo o k is prim a rily inte nde d fo r pro fe ssio na ls who wa nt to pre pa re fo r the Sun C e rtifie d P ro gra m m e r fo r Ja va 2 P la tfo rm 1.4 e x a m(re fe rre d to a s the SC P J2 e x a m ), but it is re a dily a cce ssible to a ny pro gra m m e r who wa nts to m a ste r the la ngua ge Fo r bo th purpo se s, it pro vide s a n in-de pth
co ve ra ge o f e sse ntia l fe a ture s o f the la ngua ge a nd its co re AP Is
T he re is a gre a t a nd incre a sing de m a nd fo r ce rtifie d Ja va pro gra m m e rs Sun Micro syste m s ha s de fine d the SC P J2 e x a m a s o ne tha t pro fe ssio na ls ca n ta k e to
va lida te the ir sk ills T he ce rtifica tio n pro vide s the IT industry with the sta nda rd to use fo r hiring such pro fe ssio na ls, a nd a llo ws the pro fe ssio na ls to turn the ir Ja va
sk ills into cre de ntia ls tha t a re im po rta nt fo r ca re e r a dva nce m e nt
T he bo o k he lps the re a de r m a ste r a ll co re fe a ture s o f the Ja va la ngua ge , a nd this m a ste ring o f the la ngua ge ca n culm ina te in a cco m plishing the e x a m It pro vide s
a n e x te nsive co ve ra ge o f a ll the o bje ctive s de fine d fo r the e x a m by Sun Since the e x a m o bje ctive s a re se le ctive , the y do no t co ve r m a ny o f the e sse ntia l fe a ture s
o f Ja va T his bo o k pro vide s e x te nsive co ve ra ge o f a dditio na l to pics tha t e ve ry Ja va pro gra m m e r sho uld m a ste r in o rde r to be pro ficie nt in this fie ld In this re ga rd,the bo o k is a co m pre he nsive prim e r fo r le a rning the Ja va pro gra m m ing la ngua ge
T his bo o k is not a complete reference fo r Ja va , a s it do e s no t a tte m pt to list e ve ry m e m be r o f e ve ry cla ss fro m the Ja va Syste m De ve lo pm e nt Kit (SDK) AP I
do cum e nta tio n Its purpo se is no t to do cum e nt the Ja va SDK AP Is It is a lso not a bo o k o n teaching programming techniques Its e m pha sis is o n the Ja va
pro gra m m ing la ngua ge fe a ture s, the ir synta x a nd co rre ct usa ge
T he bo o k a ssum e s a ba ck gro und in pro gra m m ing W e be lie ve the e x a m is a cce ssible to a ny pro gra m m e r who wo rk s thro ugh the bo o k A Ja va pro gra m m e r ca n
e a sily sk ip o ve r m a te ria l tha t is we ll unde rsto o d a nd co nce ntra te o n pa rts tha t ne e d re info rcing, whe re a s a pro gra m m e r ne w to Ja va will find the co nce pts e x pla ine dfro m ba sic principle s
Ea ch to pic is e x pla ine d a nd discusse d tho ro ughly with e x a m ple s, a nd ba ck e d by re vie w que stio ns a nd e x e rcise s to re info rce the co nce pts T he bo o k is no t bia se d
to wa rd a ny pa rticula r pla tfo rm , but pro vide s pla tfo rm -spe cific de ta ils whe re ne ce ssa ry
[ Team LiB ]
Trang 18Using the Book
T he re a de r ca n cho o se a line a r o r a no n-line a r ro ute thro ugh the bo o k , de pe nding o n he r pro gra m m ing ba ck gro und No n-Ja va pro gra m m e rs wishing to m igra te to
Ja va ca n re a d C ha pte r 1, which pro vide s a sho rt intro ductio n to o bje ct-o rie nte d pro gra m m ing co nce pts, a nd the pro ce dure fo r co m piling a nd running Ja va
a pplica tio ns Fo r tho se pre pa ring fo r the SC P J2 e x a m , the bo o k ha s a se pa ra te a ppe ndix pro viding a ll the pe rtine nt info rm a tio n o n ta k ing the e x a m
T he ta ble o f co nte nts, listings o f ta ble s, e x a m ple s, a nd figure s, a nd a co m pre he nsive inde x fa cilita te lo ca ting to pics discusse d in the bo o k
In pa rticula r, we dra w a tte ntio n to the fo llo wing fe a ture s o f the bo o k :
Exam and Supplementary Objectives
Ex a m o bje ctive s a re sta te d cle a rly a t the sta rt o f e ve ry cha pte r, to ge the r with a ny supple m e nta ry o bje ctive s
T he o bje ctive s a re de fine d by Sun a nd a re o rga nize d into m a jo r se ctio ns, de ta iling the curriculum fo r the e x a m
T he bo o k is o rga nize d into cha pte rs tha t lo gica lly fo llo w the o rde r o f the se m a jo r se ctio ns
T he o bje ctive s a re re pro duce d in a se pa ra te a ppe ndix whe re , fo r e a ch se ctio n o f the sylla bus, study no te s a re include d to po int the re a de r
to to pics e sse ntia l fo r the e x a m
W e be lie ve tha t the supple m e nta ry o bje ctive s a re im po rta nt to the ultim a te go a l o f m a ste ring the la ngua ge
[ Team LiB ]
Trang 19[ Team LiB ]
Review Questions
R e vie w que stio ns a re pro vide d a fte r e ve ry m a jo r to pic, in o rde r to te st a nd re info rce the m a te ria l T he se re vie w que stio ns re fle ct the k ind o f que stio ns tha t ca n be
a sk e d o n the a ctua l e x a m Anno ta te d a nswe rs to the re vie w que stio ns a re pro vide d in a se pa ra te a ppe ndix
Example 0.1 Example Source Code
W e e nco ura ge e x pe rim e nting with the co de e x a m ple s in o rde r to re info rce the m a te ria l fro m the bo o k T he se ca n be do wnlo a de d fro m the bo o k W e b site (se e
p x x x v)
Ja va co de is writte n in a mono-spaced font Line s o f co de in the e x a m ple s o r in co de snippe ts a re re fe re nce d in the te x t by a num be r, which is spe cifie d by using asingle -line co m m e nt in the co de Fo r e x a m ple , in the fo llo wing co de snippe t, the ca ll to the m e tho d doSomethingInteresting() ho pe fully do e s so m e thinginte re sting a t (1)
Trang 20Chapter Summary
Ea ch cha pte r co nclude s with a sum m a ry o f the to pics, po inting o ut the m a jo r co nce pts discusse d in the cha pte r
[ Team LiB ]
Trang 21Java 2 SDK and API Documentation
A ve rtica l gra y ba r is use d to highlight m e tho ds a nd fie lds fo und in the cla sse s o f the co re Ja va AP Is
Any e x pla na tio n fo llo wing the AP I info rm a tio n is a lso sim ila rly highlighte d
In o rde r to o bta in o ptim a l be ne fit fro m using this bo o k in pre pa ring fo r the SC P J2 e x a m , we stro ngly re co m m e nd insta lling the la te st ve rsio n (a t le a st 1.4) o f theSDK a nd its a cco m pa nying AP I do cum e nta tio n T he bo o k fo cuse s so le ly o n Ja va 2, a nd do e s no t a ck no wle dge pre vio us ve rsio ns
Java 2 Platform Upgrade Exam
Fo r tho se who ha ve ta k e n the Sun C e rtifie d P ro gra m m e r fo r Ja va 2 P la tfo rm 1.2 Ex a m , a nd wo uld lik e to pre pa re fo r the Sun C e rtifie d P ro gra m m e r fo r Ja va 2
P la tfo rm Upgra de Ex a m , we ha ve pro vide d a n a ppe ndix with de ta ils o f the upgra de e x a m T he a ppe ndix co nta ins the upgra de e x a m o bje ctive s, a nd fo r e a ch
se ctio n o f the sylla bus, study no te s a re include d to po int the re a de r to to pics e sse ntia l fo r the upgra de e x a m T he a cco m pa nying C D co nta ins m o ck e x a m s fo r theupgra de e x a m
Accompanying CD
T he a cco m pa nying C D co nta ins a we a lth o f info rm a tio n to he lp pre pa re fo r the e x a m , a nd pro vide s num e ro us m o ck e x a m s fo r the ca ndida te to te st he r sk ills T he
so ftwa re include d sim ula te s ne a r e x a m -lik e co nditio ns in o rde r to a cqua int the ca ndida te with the e x a m e nviro nm e nt Appe ndix H pro vide s de ta ils a bo ut the
co nte nts o f the C D
[ Team LiB ]
Trang 22Book Web Sites
T his bo o k is ba ck e d by W e b site s pro viding a ux ilia ry m a te ria l:
http://www.ii.uib.no /~k ha lid/pgjc2e /
http://www.a wpro fe ssio na l.co m / (se a rch fo r ISBN 0201728281)
T he co nte nts o f the W e b site s include the fo llo wing:
so urce co de fo r a ll the e x a m ple s a nd pro gra m m ing e x e rcise s in the bo o k
m o ck e x a m e ngine
e rra ta
link s to m isce lla ne o us Ja va re so urce s (ce rtifica tio n, discussio n gro ups, to o ls, e tc.)
Info rm a tio n a bo ut the Ja va 2 SDK Sta nda rd Editio n a nd its do cum e nta tio n ca n be fo und a t the fo llo wing W e b site :
http://ja va sun.co m /j2se /
T he curre nt a utho rita tive te chnica l re fe re nce fo r the Ja va pro gra m m ing la ngua ge , The Java Language Specification, Second Edition (a lso publishe d by Addiso n-W e sle y),
ca n be fo und a t this W e b site :
http://ja va sun.co m /do cs/bo o k s/jls/
[ Team LiB ]
Trang 23[ Team LiB ]
Request for Feedback
C o nside ra ble e ffo rt ha s be e n m a de to e nsure the a ccura cy o f the co nte nts o f this bo o k Se ve ra l Ja va pro fe ssio na ls ha ve pro o fre a d the m a nuscript All co de
e x a m ple s (including co de fra gm e nts) ha ve be e n co m pile d a nd te ste d o n va rio us pla tfo rm s In the fina l a na lysis, a ny e rro rs re m a ining a re the so le re spo nsibility o fthe a utho rs
Any que stio ns, co m m e nts, sugge stio ns, a nd co rre ctio ns a re we lco m e W he the r the bo o k wa s he lpful o r de trim e nta l fo r yo ur purpo se , we wo uld lik e yo ur
a sse ssm e nt Any fe e dba ck is va lua ble T he a utho rs ca n be re a che d by the fo llo wing e -m a il a lia s:
pgjc2e@ii.uib.no
[ Team LiB ]
Trang 24About the Authors
Khalid A Mughal
Kha lid A Mugha l is a n Asso cia te P ro fe sso r a t the De pa rtm e nt o f Info rm a tics a t the Unive rsity o f Be rge n, No rwa y P ro fe sso r Mugha l is re spo nsible fo r de signing a nd
im ple m e nting va rio us co urse s, which use Ja va , a t the De pa rtm e nt o f Info rm a tics O ve r the ye a rs, he ha s ta ught P ro gra m m ing La ngua ge s (Ja va , C /C ++, P a sca l),
So ftwa re Engine e ring (O bje ct-o rie nte d Syste m De ve lo pm e nt), Da ta ba se s (Da ta Mo de ling a nd Da ta ba se Ma na ge m e nt Syste m s), a nd C o m pile r T e chnique s He ha s
a lso give n num e ro us co urse s a nd se m ina rs a t va rio us le ve ls in o bje ct-o rie nte d pro gra m m ing a nd syste m de ve lo pm e nt, using Ja va a nd Ja va -re la te d te chno lo gy,
bo th a t the Unive rsity a nd fo r the IT industry He is the principa l a utho r o f the bo o k , re spo nsible fo r writing the m a te ria l co ve ring the Ja va to pics
He is a lso the principa l a utho r o f a n intro ducto ry No rwe gia n te x tbo o k o n pro gra m m ing in Ja va (Java som første programmeringsspråk/Java as First Programming
Language, C a ppe le n Ak a de m isk Fo rla g, ISBN 82-02-21782-2, 2002), which he co -a utho re d with T o rill Ha m re a nd R o lf W R a sm usse n.
His prim a ry re se a rch is in the o ry, de sign, a nd im ple m e nta tio n o f pro gra m m ing la ngua ge s a nd to o ls (P ro gra m m ing Enviro nm e nts) His curre nt wo rk invo lve s
a pplying O bje ct T e chno lo gy in the de ve lo pm e nt o f le a rning co nte nt m a na ge m e nt syste m s Fo r the pa st thre e ye a rs he ha s be e n re spo nsible fo r de ve lo ping a ndrunning a we b-ba se d pro gra m m ing co urse in Ja va , which is o ffe re d to o ff-ca m pus stude nts
He is a m e m be r o f the AC M
Rolf W Rasmussen
R o lf W R a sm usse n is a So ftwa re Engine e r, who o ve r the ye a rs ha s wo rk e d bo th a ca de m ica lly a nd pro fe ssio na lly with num e ro us pro gra m m ing la ngua ge s, including
Ja va He is e m plo ye d a s Syste m C re a to r a t vizrt, a co m pa ny tha t de ve lo ps so lutio ns fo r the T V bro a dca st industry, including re a l-tim e 3D gra phic re nde re rs, a nd
co nte nt a nd co ntro l syste m s
He m a inly wo rk s o n co ntro l a nd a uto m a tio n syste m s, vide o pro ce ssing, typo gra phy, a nd re a l-tim e visua liza tio n He ha s in the pa st wo rk e d o n cle a n ro o m
im ple m e nta tio ns o f the Ja va cla ss libra rie s, a nd is a co ntributo r to the Fre e So ftwa re Fo unda tio n
He is prim a rily re spo nsible fo r de ve lo ping the re vie w que stio ns a nd a nswe rs, the pro gra m m ing e x e rcise s a nd the ir so lutio ns, the m o ck e x a m , a nd a ll the pra ctica l
a spe cts re la te d to ta k ing the SC P J2 e x a m , pre se nte d in this bo o k
As m e ntio ne d a bo ve , he is a lso a co -a utho r o f a n intro ducto ry te x tbo o k o n pro gra m m ing in Ja va
[ Team LiB ]
Trang 25[ Team LiB ]
Acknowledgments (First Edition)
A sm a ll a pplica tio n fo r dra wing sim ple sha pe s is use d in the bo o k to illustra te va rio us a spe cts o f GUI building T he ide a fo r this a pplica tio n, a s fa r a s we k no w, first
a ppe a re d in Appe ndix D o f Data Structures and Problem Solving Using Java (M.A W e iss, Addiso n-W e sle y, 1998).
At Addiso n-W e sle y-Lo ngm a n (AW L), we wo uld lik e to tha nk Em m a Mitche ll fo r the suppo rt a nd the guida nce she pro vide d us right fro m the sta rt o f this pro je ct,
Ma rtin Klo psto ck a t AW L fo r a cco m m o da ting the no n-sta nda rd pro ce dure invo lve d in ge tting the bo o k to the printing pre ss, C live Birk s a t C R B Asso cia te s fo rpro viding the pro fe ssio na l lo o k to the co nte nts o f this bo o k , a nd fina lly, Sa lly Mo rtim o re a t AW L fo r se e ing us o ve r the finishing line T he e ffo rts o f o the r
pro fe ssio na ls be hind the sce ne s a t AW L a re a lso a ck no wle dge d
Ma ny re vie we rs ha ve be e n invo lve d during the co urse o f writing this bo o k First o f a ll, we wo uld lik e to tha nk the five a no nym o us re vie we rs co m m issio ne d by AW L to
re vie w the initia l dra ft T he ir input wa s use ful in the subse que nt writing o f this bo o k
Se ve ra l pe o ple ha ve pro vide d us with fe e dba ck o n diffe re nt pa rts o f the m a te ria l a t va rio us sta ge s: Jo n C hristia n Lø nningda l, T o rd Kå lsrud, Kje til Ive rse n, R o y
O m a , a nd Arne Lø k k e ta nge n T he ir he lp is he re by since re ly a ck no wle dge d
W e a re a lso ve ry gra te ful to La ure nce Va nhe lsuwé , Kris La po rte , Anita Ja co b, a nd T o rill Ha m re fo r ta k ing o n the da unting ta sk o f re vie wing the fina l dra ft, a ndpro viding us with e x te nsive fe e dba ck a t such sho rt no tice W e wo uld lik e to tha nk Ma rit Mugha l fo r re a ding the m a nuscript with the tra ine d e ye o f a ve te ra n Englishscho o lte a che r
W e no w unde rsta nd why fa m ily m e m be rs a re inva ria bly m e ntio ne d in a pre fa ce W itho ut o ur fa m ilie s' lo ve , suppo rt, a nd unde rsta nding this bo o k wo uld ha ve
re m a ine d a virtua l co m m o dity Kha lid wo uld lik e to tha nk Ma rit, Nina , a nd La ila fo r the ir lo ve , a nd fo r be ing his pilla rs o f suppo rt, during the writing o f this bo o k
T ha nk s a lso to the fo lk s in Birm ingha m fo r che e ring us o n R o lf wo uld lik e to tha nk Liv, R o lf V., Knut, a nd Elisa be th fo r e nduring the stra nge wo rk ing ho urspro ducing this bo o k ha s e nta ile d A spe cia l tha nk s to Ma rit fo r pro viding us with scrum ptio us dinne rs fo r co nsum ptio n a t the m idnight ho ur
[ Team LiB ]
Trang 26Acknowledgments (Second Edition)
Fe e dba ck fro m m a ny re a de rs he lpe d us to im pro ve the first e ditio n W e wo uld lik e to tha nk the fo llo wing re a de rs fo r the ir input in this e ffo rt:
Micha e l F Ado lf, T o ny Alice a , Kå re Auglænd, Jo rge L Ba rro so , Andre Be la nd, Da rre n Bruning, P a ul C a m pbe ll, R o ge r C ha ng, Jo a nna C ha ppe l, La uria n M C hirica ,Ark a di C ho ufrine , Ba rry C o lsto n, Jo hn C o tte r, Fré dé ric De m e rs, Arthur De So uza , djc, W illia m Ek ie l, Da rryl Fa illa , Jo hn Finla y, C hristo phe r R Ga rdne r, Ma rco Ga rcia ,
P e te r Gie se r, Ge o rge , P a ul Gra f, Shya m sunda r Gurura j, R a y Ho , Le o na rdo Ho la nda , Zhu Ho ngjun, Ka ra Va n Ho rn, P e te r Ho rst, Na in Hwu, Ke nt Jo hnso n, Sa m ir
Ka npa ria , O le k siy Ka rpe nk o , Je ffre y Ke nyo n, Y o ung Jin Kim , Ke nne th Kisse r, Billy Kutula s, Y i-Ming La i, R o be rt M La ngue do c, Ste ve La sle y, W inse r Lo , Na ga
Ma dipa lli, C ra ig Ma in, Avina sh Ma ndsa urwa le , T ho m a s Ma tha i, S Me hra , Y ua n Me ng, Sim o n Mille r, W illia m Mo o re , Ande rs Mo rch, Ge o rge A Mura k a m i, Sa ndy
Ne m e ce k , C hun P a n, Abiga il Ga rcía P a tiño , Anil P hilip, Alfre d R a o uf, P e te r R o rde n, C hristia n Se ife rt, Gurpre e t Singh, C hristo phe r Sta nwo o d, Swa m ina tha n
Subra m a nia n, Siva Sunda ra m , Ma nju Swa m y, Jo hn Swe e ne y, Ha rm o n T a ylo r, Andre w T o lo pk o , R a vi Ve rm a , P e r J W a lstrø m , C hun W a ng, Ja m e s W a rd, W ink y, C hun
W a ng, Jim m y Y a ng, Je nnie Y ip, Y a nqu Zho u, a nd Y ingting Zho u
At the UK o ffice o f Addiso n-W e sle y/P e a rso n Educa tio n, we wo uld lik e to tha nk o ur fo rm e r e dito r Sim o n P lum tre e fo r his unce a sing suppo rt a nd pa tie nce while weslo gge d o n with the se co nd e ditio n W e wo uld a lso lik e to a ck no wle dge the he lp a nd suppo rt o f the fo llo wing pro fe ssio na ls, pa st a nd pre se nt, a t the Lo ndo n o ffice :Aliso n Birtwe ll, Sa lly C a rte r, Ka re n Se llwo o d a nd Ka the rin Ek stro m A spe cia l tha nk s to Ka re n Mo sm a n (who ha s since m o ve d o n to a no the r jo b) fo r he r
e nco ura ge m e nt a nd a dvice
During the la st la p o f ge tting the bo o k to the printing pre ss, we we re in the ca pa ble ha nds o f Ann Se lle rs a t the US o ffice o f Addiso n-W e sle y/P e a rso n Educa tio n W e
wo uld lik e to a ck no wle dge he r e ffo rts a nd tha t o f o the r pro fe ssio na ls—in pa rticula r, Gre g Do e nch, Ja cque lyn Do uce tte , Am y Fle ische r, Micha e l Mulle n, a nd Dia nne
R usse ll—who he lpe d to ge t this bo o k thro ugh the do o r a nd o n to the bo o k she lf T ha nk s a lso to Mik e He ndrick so n fo r a lwa ys le nding a n e a r whe n we m e t a t the
O O P SLA co nfe re nce s, a nd po inting us in the right dire ctio n with o ur bo o k pla ns
W e wo uld lik e to tha nk the fo lk s a t W hizla bs So ftwa re fo r the ir co lla bo ra tio n in pro ducing the co nte nts fo r the C D a cco m pa nying this bo o k T ho se guys ce rta inly
k no w the busine ss o f de ve lo ping e x a m sim ula to rs fo r ce rtifica tio n in Ja va te chno lo gy
W e we re fo rtuna te in ha ving two Ja va gurus—La ure nce Va nhe lsuwé a nd Ma rcus Gre e n—to do the te chnica l re vie w o f the se co nd e ditio n As he did fo r the first
e ditio n, La ure nce ca m e thro ugh a nd pro vide d us with inva lua ble fe e dba ck , fro m the m inutia e o f writing te chnica l bo o k s to m a ny te chnica l issue s re la ting to the Ja vapro gra m m ing la ngua ge Ma rcus put the m a nuscript thro ugh his se ve re ce rtifica tio n scrutiny re ga rding the spe cifics o f the SC P J2 e x a m W e a re so rry to ha ve upse tthe ir pla ns fo r Ea ste r ho lida ys, a nd ha ste n to tha nk the m m o st pro fuse ly fo r ta k ing o n the ta sk
W e ca nno t tha nk e no ugh o ur o wn in-ho use , priva te co py-e dito r: Ma rit Se lje flo t Mugha l She dilige ntly a nd re le ntle ssly re a d num e ro us dra fts o f the m a nuscript,usua lly a t a ve ry sho rt no tice Ma rit cla im s tha t if she unde rsto o d wha t we ha d writte n, the n a co m pute r-lite ra te pe rso n sho uld ha ve no pro ble m wha tso e ve r T hiscla im re m a ins to be substa ntia te d If a ny co m m a s a re no t use d co rre ctly, the n it is e ntire ly o ur fa ult, in spite o f be ing re pe a te dly sho wn ho w to use the m
W e a re a lso inde bte d to m a ny Ja va -e na ble d individua ls fo r pro viding us va lua ble fe e dba ck o n pa rts o f the m a nuscript fo r the se co nd e ditio n T his include s P ra de e p
C ho pra , Se e m a R , a nd Ga ura v Ko hli a t W hizla bs So ftwa re Unfo rtuna te ly fo r us, the y o nly ha d tim e to re a d pa rt o f the m a nuscript T ha nk s a lso to T o rill Ha m re a tthe Na nse n Enviro nm e nta l a nd R e m o te Se nsing C e nte r, Be rge n, fo r he r use ful co m m e nts a nd sugge stio ns W e a lso tha nk the fo llo wing Ma ste r stude nts a t the
De pa rtm e nt o f Info rm a tics, Unive rsity o f Be rge n, fo r pro viding use ful fe e dba ck : Mik a l C a rlse n, Y ngve Espe lid, Y ngve A Aa s, Sigm und Nysæte r, T o rk e l Ho lm , a ndEsk il Sa a tve dt
Fa m ily suppo rt sa w us thro ugh this writing pro je ct a s we ll O ur fa m ilie s ha ve put up with o ur o dd a nd lo ng wo rk ing ho urs, e ndure d o ur pre o ccupa tio n a nd o ur
a bse nce a t the dining ta ble Kha lid wo uld lik e to a ck no wle dge the lo ve a nd suppo rt o f his wife , Ma rit, a nd da ughte rs, Nina a nd La ila , while wo rk ing o n this bo o k
R o lf wo uld lik e to tha nk Liv, R o lf V., Knut, a nd Elisa be th fo r the ir lo ve , pa tie nce a nd suppo rt
—Kha lid A Mugha l,
R o lf W R a sm usse n
March 2003
Bergen, Norway
[ Team LiB ]
Trang 27[ Team LiB ]
Chapter 1 Basics of Java Programming
Supplementary Objectives
Intro duce the ba sic te rm ino lo gy a nd co nce pts in o bje ct-o rie nte d pro gra m m ing: cla sse s, o bje cts, re fe re nce s, fie lds, m e tho ds, m e m be rs,
inhe rita nce , a ggre ga tio n
Ide ntify the e sse ntia l e le m e nts o f a Ja va a pplica tio n
Le a rn ho w to co m pile a nd run a Ja va a pplica tio n
[ Team LiB ]
Trang 281.1 Introduction
Be fo re e m ba rk ing o n the ro a d to Ja va pro gra m m e r ce rtifica tio n, it is im po rta nt to unde rsta nd the ba sic te rm ino lo gy a nd co nce pts in o bje ct-o rie nte d pro gra m m ing(O O P ) In this cha pte r, the e m pha sis is o n pro viding a n intro ductio n ra the r tha n a n e x ha ustive co ve ra ge In-de pth co ve ra ge o f the co nce pts fo llo ws in due co urse insubse que nt cha pte rs o f the bo o k
Ja va suppo rts the writing o f m a ny diffe re nt k inds o f e x e cuta ble s: a pplica tio ns, a pple ts, a nd se rvle ts T he ba sic e le m e nts o f a Ja va a pplica tio n a re intro duce d in thischa pte r T he o ld a da ge tha t pra ctice m a k e s pe rfe ct is ce rta inly true whe n le a rning a pro gra m m ing la ngua ge T o e nco ura ge pro gra m m ing o n the co m pute r, the
m e cha nics o f co m piling a nd running a Ja va a pplica tio n a re o utline d
[ Team LiB ]
Trang 29[ Team LiB ]
1.2 Classes
O ne o f the funda m e nta l wa ys in which we ha ndle co m ple x ity is abstractions An a bstra ctio n de no te s the e sse ntia l pro pe rtie s a nd be ha vio rs o f a n o bje ct tha t
diffe re ntia te it fro m o the r o bje cts T he e sse nce o f O O P is m o de lling a bstra ctio ns, using cla sse s a nd o bje cts T he ha rd pa rt in this e nde a vo ur is finding the right
a bstra ctio ns
A class de no te s a ca te go ry o f o bje cts, a nd a cts a s a blue print fo r cre a ting such o bje cts A cla ss m o de ls a n a bstra ctio n by de fining the pro pe rtie s a nd be ha vio rs fo r
the o bje cts re pre se nting the a bstra ctio n An o bje ct e x hibits the pro pe rtie s a nd be ha vio rs de fine d by its cla ss T he pro pe rtie s o f a n o bje ct o f a cla ss a re a lso ca lle d
attributes, a nd a re de fine d by fields in Ja va A field in a cla ss de finitio n is a va ria ble which ca n sto re a va lue tha t re pre se nts a pa rticula r pro pe rty T he be ha vio rs o f
a n o bje ct o f a cla ss a re a lso k no wn a s operations, a nd a re de fine d using methods in Ja va Fie lds a nd m e tho ds in a cla ss de finitio n a re co lle ctive ly ca lle d members.
An im po rta nt distinctio n is m a de be twe e n the contract a nd the implementation tha t a cla ss pro vide s fo r its o bje cts T he co ntra ct de fine s what se rvice s, a nd the
im ple m e nta tio n de fine s how the se se rvice s a re pro vide d by the cla ss C lie nts (i.e , o the r o bje cts) o nly ne e d to k no w the co ntra ct o f a n o bje ct, a nd no t its
im ple m e nta tio n, in o rde r to a va il the m se lve s o f the o bje ct's se rvice s
As a n e x a m ple , we will im ple m e nt diffe re nt ve rsio ns o f a cla ss tha t m o de ls the a bstra ctio n o f a sta ck tha t ca n push a nd po p cha ra cte rs T he sta ck will use a n a rra y
o f cha ra cte rs to sto re the cha ra cte rs, a nd a fie ld to indica te the to p e le m e nt in the sta ck Using Unifie d Mo de ling La ngua ge (UML) no ta tio n, a cla ss ca lle d C harStack
is gra phica lly de picte d in Figure 1.1, which m o de ls the a bstra ctio n Bo th fie lds a nd m e tho d na m e s a re sho wn in Figure 1.1a
Figure 1.1 UML Notation for Classes
gra phics/01fig01.gif
Declaring Members: Fields and Methods
Ex a m ple 1.1 sho ws the de finitio n o f the cla ss C harStack de picte d in Figure 1.1 Its inte ntio n is to illustra te the sa lie nt fe a ture s o f a cla ss de finitio n in Ja va , a nd no t
e ffe ctive im ple m e nta tio n o f sta ck s
A cla ss de finitio n co nsists o f a se rie s o f m e m be r de cla ra tio ns In the ca se o f the cla ss C harStack, it ha s two fie lds:
stack A rray, which is a n a rra y to ho ld the e le m e nts o f the sta ck (in this ca se cha ra cte rs)
topO fStack, which de no te s the to p e le m e nt o f the sta ck (i.e , inde x o f the la st cha ra cte r sto re d in the a rra y)
T he cla ss C harStack ha s five m e tho ds tha t im ple m e nt the e sse ntia l o pe ra tio ns o n a sta ck :
push() pushe s a cha ra cte r o n to the sta ck
pop() re m o ve s a nd re turns the to p e le m e nt o f the sta ck
peek () re turns the to p e le m e nt o f the sta ck fo r inspe ctio n
isEmpty () de te rm ine s whe the r the sta ck is e m pty
isFull() de te rm ine s whe the r the sta ck is full
T he cla ss de finitio n a lso ha s a m e tho d-lik e de cla ra tio n with the sa m e na m e a s the cla ss, (2) Such de cla ra tio ns a re ca lle d constructors As we sha ll se e , a
co nstructo r is e x e cute d whe n a n o bje ct is cre a te d fro m the cla ss Ho we ve r, the im ple m e nta tio n de ta ils in the e x a m ple a re no t im po rta nt fo r the pre se nt discussio n
Example 1.1 Basic Elements of a Class Definition
// Source Filename: C harStack jav a
public class C harStack { // C lass name
// C lass Declarations:
// (1) Fields:
priv ate char[] stack A rray ; // The array implementing the stack
priv ate int topO fStack ; // The top of the stack
// (2) C onstructor:
public C harStack (int n) { stack A rray = new char[n]; topO fStack = -1; }
// (3) Methods:
public v oid push(char element) { stack A rray [++topO fStack ] = element; }
public char pop() { return stack A rray [topO fStack ]; }
public char peek () { return stack A rray [topO fStack ]; }
public boolean isEmpty () { return topO fStack < 0; }
public boolean isFull() { return topO fStack == stack A rray length - 1; }
}
[ Team LiB ]
Trang 301.3 Objects
Class Instantiation
T he pro ce ss o f cre a ting o bje cts fro m a cla ss is ca lle d instantiation An object is a n insta nce o f a cla ss T he o bje ct is co nstructe d using the cla ss a s a blue print a nd is
a co ncre te insta nce o f the a bstra ctio n tha t the cla ss re pre se nts An o bje ct m ust be cre a te d be fo re it ca n be use d in a pro gra m In Ja va , o bje cts a re m a nipula te d
thro ugh object references (a lso ca lle d reference values o r sim ply references) T he pro ce ss o f cre a ting o bje cts usua lly invo lve s the fo llo wing ste ps:
1 De cla ra tio n o f a va ria ble to sto re the o bje ct re fe re nce
T his invo lve s de cla ring a reference variable o f the a ppro pria te cla ss to sto re the re fe re nce to the o bje ct.
// Declaration of two reference v ariables that will denote
// two distinct objects, namely two stack s of characters, respectiv ely
C harStack stack 1, stack 2;
2 C re a ting a n o bje ct.
T his invo lve s using the new o pe ra to r in co njunctio n with a ca ll to a co nstructo r, to cre a te a n insta nce o f the cla ss
// C reate two distinct stack s of chars
stack 1 = new C harStack (10); // Stack length: 10 chars
stack 2 = new C harStack (5); // Stack length: 5 chars
T he new o pe ra to r re turns a re fe re nce to a ne w insta nce o f the C harStack cla ss T his re fe re nce ca n be a ssigne d to a re fe re nce va ria ble o f the a ppro pria te cla ss
Ea ch o bje ct ha s a unique ide ntity a nd ha s its o wn co py o f the fie lds de cla re d in the cla ss de finitio n T he two sta ck s, de no te d by stack 1 a nd stack 2, will ha vethe ir o wn stack A rray a nd topO fStack fie lds
T he purpo se o f the co nstructo r ca ll o n the right side o f the new o pe ra to r is to initia lize the ne wly cre a te d o bje ct In this pa rticula r ca se , fo r e a ch ne w C harStack
insta nce cre a te d using the new o pe ra to r, the co nstructo r cre a te s a n a rra y o f cha ra cte rs T he le ngth o f this a rra y is give n by the va lue o f the a rgum e nt to the
co nstructo r T he co nstructo r a lso initia lize s the topO fStack fie ld
T he de cla ra tio n a nd the insta ntia tio n ca n a lso be co m bine d:
C harStack stack 1 = new C harStack (10),
stack 2 = new C harStack (5);
Figure 1.2 sho ws the UML no ta tio n fo r o bje cts T he gra phica l re pre se nta tio n o f a n o bje ct is ve ry sim ila r to tha t o f a cla ss Figure 1.2 sho ws the ca no nica l no ta tio n,whe re the na m e o f the re fe re nce va ria ble de no ting the o bje ct is pre fix e d to the cla ss na m e with a co lo n ':' If the na m e o f the re fe re nce va ria ble is o m itte d, a s inFigure 1.2b, this de no te s a n a no nym o us o bje ct Since o bje cts in Ja va do no t ha ve na m e s, but a re de no te d by re fe re nce s, a m o re e la bo ra te no ta tio n is sho wn inFigure 1.2c, whe re o bje cts re pre se nting re fe re nce s o f C harStack cla ss e x plicitly re fe r to C harStack o bje cts In m o st ca se s, the m o re co m pa ct no ta tio n will suffice
Figure 1.2 UML Notation for Objects
gra phics/01fig02.gif
Object References
A re fe re nce pro vide s a ha ndle to a n o bje ct tha t is cre a te d a nd sto re d in m e m o ry In Ja va , o bje cts ca n o nly be m a nipula te d via re fe re nce s, which ca n be sto re d in
va ria ble s An o bje ct ca n ha ve se ve ra l re fe re nce s, o fte n ca lle d its aliases T he o bje ct ca n be m a nipula te d via a ny o ne o f its a lia se s.
// C reate two distinct stack s of chars
C harStack stack A = new C harStack (12); // Stack length: 12 chars
C harStack stack B = new C harStack (6); // Stack length: 6 chars
stack B = stack A ; // (1) aliases after assignment
// Stack prev iously referenced by stack B can now be garbage collected
T wo sta ck s a re cre a te d in the co de a bo ve Be fo re the a ssignm e nt a t (1), the situa tio n is a s de picte d in Figure 1.3a Afte r the a ssignm e nt a t (1), re fe re nce va ria ble s
stack A a nd stack B will de no te the sa m e sta ck , a s de picte d in Figure 1.3b R e fe re nce va ria ble s stack A a nd stack B a re a lia se s a fte r the a ssignm e nt, a s the y re fe r to the
sa m e o bje ct W ha t ha ppe ns to the sta ck o bje ct tha t wa s de no te d by the re fe re nce va ria ble stack B be fo re the a ssignm e nt? W he n o bje cts a re no lo nge r in use , the ir
m e m o ry is, if ne ce ssa ry, re cla im e d a nd re a llo ca te d fo r o the r o bje cts T his is ca lle d automatic garbage collection Ga rba ge co lle ctio n in Ja va is ta k e n ca re o f by the
runtim e syste m
Figure 1.3 Aliases
Trang 31[ Team LiB ]
Trang 321.4 Instance Members
Ea ch o bje ct cre a te d will ha ve its o wn co pie s o f the fie lds de fine d in its cla ss T he fie lds o f a n o bje ct a re ca lle d instance variables T he va lue s o f the insta nce va ria ble s
in a n o bje ct co m prise its state T wo distinct o bje cts ca n ha ve the sa m e sta te , if the ir insta nce va ria ble s ha ve the sa m e va lue s T he m e tho ds o f a n o bje ct de fine its
be ha vio r T he se m e tho ds a re ca lle d instance methods It is im po rta nt to no te tha t the se m e tho ds pe rta in to e a ch o bje ct o f the cla ss T his sho uld no t be co nfuse d
with the im ple m e nta tio n o f the m e tho ds, which is sha re d by a ll insta nce s o f the cla ss Insta nce va ria ble s a nd insta nce m e tho ds, which be lo ng to o bje cts, a re
co lle ctive ly ca lle d instance members, to distinguish the m fro m static members, which o nly be lo ng to the cla ss Sta tic m e m be rs a re discusse d in Se ctio n 1.5.
Invoking Methods
O bje cts co m m unica te by m e ssa ge pa ssing T his m e a ns tha t a n o bje ct ca n be m a de to e x hibit a pa rticula r be ha vio r by invo k ing the a ppro pria te o pe ra tio n o n the
o bje ct In Ja va , this is do ne by calling a m e tho d o n the o bje ct using the bina ry infix do t '.' o pe ra to r A method call spe lls o ut the co m ple te m e ssa ge : the o bje ct tha t
is the re ce ive r o f the m e ssa ge , the m e tho d to be invo k e d, a nd the a rgum e nts to the m e tho d, if a ny T he m e tho d invo k e d o n the re ce ive r ca n a lso se nd
info rm a tio n ba ck to the se nde r, via a re turn va lue T he m e tho d ca lle d m ust be o ne tha t is de fine d fo r the o bje ct
C harStack stack = new C harStack (5); // C reate a stack
stack push('J'); // (1) C haracter 'J' pushed
char c = stack pop(); // (2) O ne character popped and returned: 'J'
stack printStack Elements(); // (3) C ompile time error: No such method in C harStack
T he sa m ple co de a bo ve invo k e s m e tho ds o n the o bje ct de no te d by the re fe re nce va ria ble stack T he m e tho d ca ll a t (1) pushe s o ne cha ra cte r o n the sta ck , a nd the
m e tho d ca ll a t (2) po ps o ne cha ra cte r o ff the sta ck Bo th push() a nd pop() m e tho ds a re de fine d in the cla ss C harStack T he push() m e tho d do e s no t re turn a ny va lue ,but the pop() m e tho d re turns the cha ra cte r po ppe d T rying to invo k e a m e tho d printStack Elements() o n the sta ck re sults in a co m pile -tim e e rro r, a s no such m e tho d is
de fine d in the cla ss C harStack
T he do t '.' no ta tio n a lso ca n be use d with a re fe re nce to a cce ss fie lds o f a n o bje ct T he use o f the do t no ta tio n is go ve rne d by the accessibility o f the m e m be r T he
fie lds in cla ss C harStack ha ve priv ate a cce ssibility, indica ting tha t the y a re no t a cce ssible fro m o utside the cla ss:
stack topO fStack ++; // C ompile time error: topO fStack is a priv ate field
[ Team LiB ]
Trang 33[ Team LiB ]
1.5 Static Members
In so m e ca se s, ce rta in m e m be rs sho uld o nly be lo ng to the cla ss, a nd no t be pa rt o f a ny o bje ct cre a te d fro m the cla ss An e x a m ple o f such a situa tio n is whe n acla ss wa nts to k e e p tra ck o f ho w m a ny o bje cts o f the cla ss ha ve be e n cre a te d De fining a co unte r a s a n insta nce va ria ble in the cla ss de finitio n fo r tra ck ing thenum be r o f o bje cts cre a te d, do e s no t so lve the pro ble m Ea ch o bje ct cre a te d will ha ve its o wn co unte r fie ld W hich co unte r sho uld the n be upda te d? T he so lutio n is
to de cla re the co unte r fie ld a s be ing static Such a fie ld is ca lle d a static variable It be lo ngs to the cla ss, a nd no t to a ny o bje ct o f the cla ss A sta tic va ria ble is initia lize d whe n the cla ss is lo a de d a t runtim e Sim ila rly, a cla ss ca n ha ve static methods tha t be lo ng o nly to the cla ss, a nd no t to a ny o bje cts o f the cla ss Sta tic
va ria ble s a nd sta tic m e tho ds a re co lle ctive ly k no wn a s static members, a nd a re distinguishe d fro m insta nce m e m be rs in a cla ss de finitio n by the k e ywo rd static inthe ir de cla ra tio n
Figure 1.4 sho ws the cla ss dia gra m fo r the cla ss C harStack It ha s be e n a ugm e nte d by two sta tic m e m be rs tha t a re sho wn unde rline d T he a ugm e nte d de finitio n o fthe C harStack cla ss is give n in Ex a m ple 1.2 T he fie ld counter is a sta tic va ria ble de cla re d a t (1) It will be a llo ca te d a nd initia lize d to the de fa ult va lue 0 whe n thecla ss is lo a de d Ea ch tim e a n o bje ct o f the C harStack cla ss is cre a te d, the co nstructo r a t (2) is e x e cute d T he co nstructo r e x plicitly incre m e nts the co unte r in the cla ss
T he m e tho d getInstanceC ount() a t (3) is a sta tic m e tho d be lo nging to the cla ss It re turns the co unte r va lue whe n ca lle d
Figure 1.4 Class Diagram Showing Static Members of a Class
gra phics/01fig04.gif
Figure 1.5 sho ws the cla ssifica tio n o f the m e m be rs in cla ss C harStack using the te rm ino lo gy we ha ve intro duce d so fa r T a ble 1.1 a t the e nd o f this se ctio n, pro vide s
a sum m a ry o f the te rm ino lo gy use d in de fining m e m be rs o f a cla ss
Example 1.2 Static Members in Class Definition
// Source Filename C harStack jav a
public class C harStack {
// Instance v ariables
priv ate char[] stack A rray ; // The array implementing the stack
priv ate int topO fStack ; // The top of the stack
// Static v ariable
priv ate static int counter; // (1)
// C onstructor now increments the counter for each object created
public C harStack (int capacity ) { // (2)
stack A rray = new char[capacity ];
topO fStack = -1;
counter++;
}
// Instance methods
public v oid push(char element) { stack A rray [++topO fStack ] = element; }
public char pop() { return stack A rray [topO fStack ]; }
public char peek () { return stack A rray [topO fStack ]; }
public boolean isEmpty () { return topO fStack < 0; }
public boolean isFull() { return topO fStack == stack A rray length - 1; }
// Static method (3)
public static int getInstanceC ount() { return counter; }
}
Figure 1.5 Members of a Class
C lie nts ca n a cce ss sta tic m e m be rs in the cla ss by using the cla ss na m e T he fo llo wing co de invo k e s the getInstanceC ount() m e tho d in the cla ss C harStack:
int count = C harStack getInstanceC ount(); // C lass name to inv ok e static method
Sta tic m e m be rs ca n a lso be a cce sse d via o bje ct re fe re nce s:
Trang 34Sta tic m e m be rs in a cla ss ca n be a cce sse d bo th by the cla ss na m e a nd via o bje ct re fe re nce s, but insta nce m e m be rs ca n o nly be a cce sse d by o bje ct re fe re nce s.
Table 1.1 Terminology for Class Members
A fie ld tha t is a llo ca te d whe n the cla ss is insta ntia te d, tha t is, whe n a n o bje ct) o f the cla ss is cre a te d Also ca lle d non-static field
Insta nce Me tho d A m e tho d tha t be lo ngs to a n insta nce o f the cla ss O bje cts o f the sa m e cla ss sha re its im ple m e nta tio n
Sta tic Me m be rs T he se a re sta tic va ria ble s a nd sta tic m e tho ds o f a cla ss T he y ca n be a cce sse d o r invo k e d e ithe r by using the cla ss na m e o r thro ugh a n o bje ct
re fe re nce
Sta tic Va ria ble A fie ld tha t is a llo ca te d whe n the cla ss is lo a de d It be lo ngs to the cla ss a nd no t to a ny o bje ct o f the cla ss Also ca lle d static field a nd class v ariable.Sta tic Me tho d A m e tho d which be lo ngs to the cla ss a nd no t to a ny o bje ct o f the cla ss Also ca lle d class method
[ Team LiB ]
Trang 35[ Team LiB ]
1.6 Inheritance
T he re a re two funda m e nta l m e cha nism s fo r building ne w cla sse s fro m e x isting o ne s: inheritance a nd aggregation It m a k e s se nse to inherit fro m a n e x isting cla ss
Vehicle to de fine a cla ss C ar, since a ca r is a ve hicle T he cla ss Vehicle ha s se ve ra l parts; the re fo re , it m a k e s se nse to de fine a composite object o f cla ss Vehicle tha t ha s
constituent objects o f such cla sse s a s Motor, A xle, a nd GearBox, which m a k e up a ve hicle
Inhe rita nce is illustra te d by a n e x a m ple tha t im ple m e nts a sta ck o f cha ra cte rs tha t ca n print its e le m e nts o n the te rm ina l T his ne w sta ck ha s a ll the pro pe rtie s a nd
be ha vio rs o f the C harStack cla ss, but it a lso ha s the a dditio na l ca pa bility o f printing its e le m e nts Give n tha t this printa ble sta ck is a sta ck o f cha ra cte rs, it ca n be
de rive d fro m the C harStack cla ss T his re la tio nship is sho wn in Figure 1.6 T he cla ss PrintableC harStack is ca lle d the subclass, a nd the cla ss C harStack is ca lle d the
superclass T he C harStack cla ss is a generalization fo r a ll sta ck s o f cha ra cte rs, whe re a s the cla ss PrintableC harStack is a specialization o f sta ck s o f cha ra cte rs tha t ca n a lso
print the ir e le m e nts
Figure 1.6 Class Diagram Depicting Inheritance Relation
gra phics/01fig06.gif
In Ja va , de riving a ne w cla ss fro m a n e x isting cla ss re quire s the use o f the extends cla use in the subcla ss de finitio n A subcla ss ca n extend o nly o ne supe rcla ss T he
subcla ss inhe rits m e m be rs o f the supe rcla ss T he fo llo wing co de fra gm e nt im ple m e nts the PrintableC harStack cla ss:
class PrintableC harStack extends C harStack { // (1)
// Instance method
public v oid printStack Elements() { // (2)
// implementation of the method
}
// The constructor calls the constructor of the superclass explicitly
public PrintableC harStack (int capacity ) { super(capacity ); } // (3)
}
T he PrintableC harStack cla ss e x te nds the C harStack cla ss a t (1) Im ple m e nting the printStack Elements() m e tho d in the PrintableC harStack cla ss re quire s a cce ss to the fie ld
stack A rray fro m the supe rcla ss C harStack Ho we ve r, this fie ld is private a nd the re fo re no t a cce ssible in the subcla ss T he subcla ss ca n a cce ss the se fie lds if the
a cce ssibility o f the fie lds is cha nge d to protected in the C harStack cla ss Ex a m ple 1.3 use s a ve rsio n o f the cla ss C harStack, which ha s be e n m o difie d a cco rdingly
Im ple m e nta tio n o f the printStack Elements() m e tho d is sho wn a t (2) T he co nstructo r o f the PrintableC harStack cla ss a t (3) ca lls the co nstructo r o f the supe rcla ss C harStack
in o rde r to initia lize the sta ck pro pe rly
Example 1.3 Defining a Subclass
// Source Filename: C harStack jav a
public class C harStack {
// Instance v ariables
protected char[] stack A rray ; // The array that implements the stack
protected int topO fStack ; // The top of the stack
// The rest of the definition is the same as in Example 1.2
}
// Source Filename: PrintableC harStack jav a
public class PrintableC harStack extends C harStack { // (1)
// Instance method
public v oid printStack Elements() { // (2)
for (int i = 0; i <= topO fStack ; i++)
Sy stem.out.print(stack A rray [i]); // print each char on terminal
Sy stem.out.println();
}
// C onstructor calls the constructor of the superclass explicitly
PrintableC harStack (int capacity ) { super(capacity ); } // (3)
}
O bje cts o f the PrintableC harStack cla ss will re spo nd just lik e the o bje cts o f the C harStack cla ss, but the y will a lso ha ve the a dditio na l functio na lity de fine d in thesubcla ss:
PrintableC harStack aPrintableC harStack = new PrintableC harStack (3);
aPrintableC harStack push('H');
aPrintableC harStack push('i');
aPrintableC harStack push('!');
aPrintableC harStack printStack Elements(); // Prints "Hi!" on the terminal
[ Team LiB ]
Trang 361.7 Aggregation
W he n building ne w cla sse s fro m e x isting cla sse s using aggregation, a co m po site o bje ct is built fro m o the r co nstitue nt o bje cts tha t a re its pa rts.
Ja va suppo rts a ggre ga tio n o f o bje cts by re fe re nce , since o bje cts ca nno t co nta in o the r o bje cts e x plicitly T he fie lds ca n o nly co nta in va lue s o f prim itive da ta type s o r
re fe re nce s to o the r o bje cts Ea ch o bje ct o f the C harStack cla ss ha s a fie ld to sto re the re fe re nce to a n a rra y o bje ct tha t ho lds the cha ra cte rs Ea ch sta ck o bje ct a lso
ha s a fie ld o f prim itive da ta type int to sto re the inde x va lue tha t de no te s the to p o f sta ck T his is re fle cte d in the de finitio n o f the C harStack cla ss, which co nta ins a ninsta nce va ria ble fo r e a ch o f the se pa rts In co ntra st to the co nstitue nt o bje cts who se re fe re nce s a re sto re d in fie lds, the va lue s o f prim itive da ta type s a re sto re d in
the fie lds o f the co m po site o bje ct T he aggregation re la tio nship is de picte d by the UML dia gra m in Figure 1.7, sho wing tha t e a ch o bje ct o f the C harStack cla ss will ha ve
o ne a rra y o bje ct o f char a sso cia te d with it
Figure 1.7 Class Diagram Depicting Aggregation
gra phics/01fig07.gif
[ Team LiB ]
Trang 37[ Team LiB ]
1.8 Tenets of Java
C o de in Ja va m ust be e nca psula te d in cla sse s
T he re a re two k inds o f va lue s in Ja va : o bje ct re fe re nce s a nd a to m ic va lue s o f prim itive type s
R e fe re nce s de no te o bje cts tha t a re cre a te d fro m cla sse s
O bje cts ca n o nly be m a nipula te d via re fe re nce s
O bje cts in Ja va ca nno t co nta in o the r o bje cts; the y ca n o nly ha ve re fe re nce s to o the r o bje cts
De le tio n o f o bje cts is m a na ge d by the runtim e syste m
[ Team LiB ]
Trang 38Review Questions
1.1 W hich sta te m e nt is true a bo ut a m e tho d?
Se le ct the o ne co rre ct a nswe r
a A m e tho d is a n im ple m e nta tio n o f a n a bstra ctio n.
b A m e tho d is a n a ttribute de fining the pro pe rty o f a pa rticula r a bstra ctio n.
c A m e tho d is a ca te go ry o f o bje cts.
d A m e tho d is a n o pe ra tio n de fining the be ha vio r fo r a pa rticula r a bstra ctio n.
e A m e tho d is a blue print fo r m a k ing o pe ra tio ns.
1.2 W hich sta te m e nt is true a bo ut a n o bje ct?
Se le ct the o ne co rre ct a nswe r
a An o bje ct is wha t cla sse s a re insta ntia te d fro m
b An o bje ct is a n insta nce o f a cla ss.
c An o bje ct is a blue print fo r cre a ting co ncre te re a liza tio n o f a bstra ctio ns.
d An o bje ct is a re fe re nce to a n a ttribute
e An o bje ct is a va ria ble
1.3 W hich line co nta ins a co nstructo r in this cla ss de finitio n?
public class C ounter { // (1)
int current, step;
public C ounter(int startValue, int stepValue) { // (2)
set(startValue);
setStepValue(stepValue);
}
public int get() { return current; } // (3)
public v oid set(int v alue) { current = v alue; } // (4)
public v oid setStepValue(int stepValue) { step = stepValue; } // (5)
1.4 Give n tha t Thing is a cla ss, ho w m a ny o bje cts a nd ho w m a ny re fe re nce va ria ble s a re cre a te d by the fo llo wing co de ?
Thing item, stuff;
item = new Thing();
Thing entity = new Thing();
Se le ct the two co rre ct a nswe rs
a O ne o bje ct is cre a te d.
b T wo o bje cts a re cre a te d.
c T hre e o bje cts a re cre a te d.
d O ne re fe re nce va ria ble is cre a te d.
e T wo re fe re nce va ria ble s a re cre a te d.
f T hre e re fe re nce va ria ble s a re cre a te d.
1.5 W hich sta te m e nt is true a bo ut a n insta nce m e tho d?
Se le ct the o ne co rre ct a nswe r
a An insta nce m e m be r is a lso ca lle d a sta tic m e m be r.
b An insta nce m e m be r is a lwa ys a fie ld.
c An insta nce m e m be r is ne ve r a m e tho d.
d An insta nce m e m be r be lo ngs to a n insta nce , no t to the cla ss a s a who le
e An insta nce m e m be r a lwa ys re pre se nts a n o pe ra tio n.
1.6 Ho w do o bje cts pa ss m e ssa ge s in Ja va ?
Se le ct the o ne co rre ct a nswe r
a T he y pa ss m e ssa ge s by m o difying e a ch o the r's fie lds.
b T he y pa ss m e ssa ge s by m o difying the sta tic va ria ble s o f e a ch o the r's cla sse s.
Trang 391.7 Give n the fo llo wing co de , which sta te m e nts a re true ?
b C la ss B is the supe rcla ss o f cla ss A
c C la ss A inhe rits fro m cla ss B
d C la ss B is a subcla ss o f cla ss A
e O bje cts o f cla ss A ha ve a fie ld na m e d v alue2
f O bje cts o f cla ss B ha ve a fie ld na m e d v alue1
[ Team LiB ]
Trang 401.9 Java Programs
A Ja va pro gra m is a co lle ctio n o f o ne o r m o re cla sse s, with o ne o f the m co nta ining the pro gra m 's e x e cutio n sta rting po int A Ja va source file ca n co nta in m o re tha n
o ne cla ss de finitio n T he Ja va 2 SDK e nfo rce s the rule tha t a t the m o st o ne cla ss in the so urce file ha s public a cce ssibility T he na m e o f the so urce file is co m prise d
o f the na m e o f this public cla ss with jav a a s e x te nsio n Ea ch cla ss de finitio n in a so urce file is co m pile d into a se pa ra te class file, co nta ining Java byte code T he
na m e o f this file is co m prise d o f the na m e o f the cla ss with class a s a n e x te nsio n All pro gra m s m ust be co m pile d be fo re the y ca n be run T he Ja va 2 SDK pro vide s
to o ls fo r this purpo se , a s e x pla ine d in 1.10 Sa m ple Ja va Applica tio n
[ Team LiB ]