Java Programming Language Past Paper PDF
Document Details
Uploaded by Deleted User
2005
Tags
Summary
This document contains course material for a Java Programming course, covering sections like getting started and object-oriented programming. The content includes headers and descriptions of the areas, highlighting different segments of the course, such as object-oriented programming and Java Programming.
Full Transcript
Sun Services Java™ Programming Language SL-275 Java™ Programming Language cè¸hfl8HFb[QOO´[tC”[n84flèdhdb5«d™[j”4M™[Bê´O[o5bEèfl,[c8fl4é5™[t»”b»[cé»fl»™[c»é86èfl”8»[I´O´B™[uMtM-M[-éé[fl8HFbd fl5d5flD5–M 7F8d ¸flè–C4b èfl –è4C«5”b 8d ¸flèb54b5– 3h 4è¸hfl8HFb...
Sun Services Java™ Programming Language SL-275 Java™ Programming Language cè¸hfl8HFb[QOO´[tC”[n84flèdhdb5«d™[j”4M™[Bê´O[o5bEèfl,[c8fl4é5™[t»”b»[cé»fl»™[c»é86èfl”8»[I´O´B™[uMtM-M[-éé[fl8HFbd fl5d5flD5–M 7F8d ¸flè–C4b èfl –è4C«5”b 8d ¸flèb54b5– 3h 4è¸hfl8HFb »”– –8dbfl83Cb5– C”–5fl é845”d5d fl5dbfl84b8”H 8bd Cd5™ 4è¸h8”H™ –8dbfl83Cb8è”™ »”– –54諸8é»b8è”M oè ¸»flb è6 bF8d ¸flè–C4b èfl –è4C«5”b «»h 35[fl5¸flè–C45–[8”[»”h[6èfl«[3h[»”h[«5»”d[E8bFèCb[¸fl8èfl[Efl8bb5”[»CbFèfl8G»b8è”[è6[tC”[»”–[8bd[é845”dèfld™[86[»”hM 7F8fl–m¸»flbh[dè6bE»fl5™ 8”4éC–8”H[6è”b[b54F”èéèHh™[8d[4è¸hfl8HFb5–[»”–[é845”d5–[6flè«[tC”[dC¸¸é85fldM tC”™ tC” n84flèdhdb5«d™ bF5 tC” AèHè™ k»D»™ k»D»`5»”d™ k»D»lt™ ke`c™ ke1™ kvn™ kQtf™ k8”8™ »”– tèé»fl8d »fl5 bfl»–5«»fl,d èfl fl5H8db5fl5– bfl»–5«»fl,d è6 tC” n84flèdhdb5«d™ j”4M 8” bF5 uMtM »”– èbF5fl[4èC”bfl85dM uojy[8d[»[fl5H8db5fl5–[bfl»–5«»fl,[8”[bF5[uMtM[»”–[èbF5fl[4èC”bfl85d™[5J4éCd8D5éh[é845”d5–[bFflèCHF[yNl¸5”[c諸»”h™ Ab–M o5bd4»¸5[o»D8H»bèfl[8d[»[bfl»–5«»fl,[è6[o5bd4»¸5[cè««C”84»b8è”d[cèfl¸èfl»b8è”M 7F5 lfifo All1 »”– tC” ifl»¸F84»é ud5fl j”b5fl6»45 E»d –5D5éè¸5– 3h tC” n84flèdhdb5«d™ j”4M 6èfl 8bd Cd5fld »”– é845”d55dM tC” »4,”èEé5–H5d bF5 ¸8è”55fl8”H 566èflbd è6 y5flèJ 8” fl5d5»fl4F8”H »”– –5D5éè¸8”H bF5 4è”45¸b è6 D8dC»é èfl Hfl»¸F84»é Cd5fl 8”b5fl6»45d 6èfl bF5 4諸Cb5fl 8”–CdbflhM tC” Fèé–d » ”è”m5J4éCd8D5 é845”d5 6flè« y5flèJ bè bF5 y5flèJ ifl»¸F84»é ud5fl j”b5fl6»45™ EF84F é845”d5[»édè[4èD5fld[tC”Ud[é845”d55d[EFè[8«¸é5«5”b[lfifo[All1[iujd[»”–[èbF5flE8d5[4諸éh[E8bF[tC”Ud[Efl8bb5”[é845”d5[»Hfl55«5”bdM uMtM[ièD5fl”«5”b[»¸¸flèD»é[«8HFb[35[fl5PC8fl5–[EF5”[5J¸èflb8”H[bF5[¸flè–C4bM sft7sjc7fe sji“7tx ud5™ –C¸é84»b8è”™ èfl –8d4éèdCfl5 3h bF5 uMtM ièD5fl”«5”b 8d dC3+54b bè fl5dbfl84b8è”d è6 T-s ´QMQQımêBVHWVQWVˆNÇıW »”– T-s ´QMQQımêIVˆNÇıW™ èfl eT-s Q´QMQQımıOê´ V3WVˆNI´W[»”–[eT-s[QQıMıQOQmRV»WM elcunfo7-7jlo jt fislvjefe 9-t jt; -oe -AA fyfisftt ls jnfiAjfe cloej7jlot™ sffisftfo7-7jlot™ -oe w-ss-o7jft™ jocAuejoi -o jnfiAjfe w-ss-o7 lT nfsc“-o7-`jAj7 ™ Tj7oftt Tls - fi-s7jcuA-s fiusfiltf ls olomjoTsjoifnfo7™ -sf ejtcA-jnfe™ fycffi7 7l 7“f fy7fo7 7“-7 tuc“ ejtcA-jnfst -sf “fAe[7l[`f[Afi-AA [jov-AjeM fJ¸èflb[cè««è–8bh[cé»dd8684»b8è”[oC«35fl[-dd8H”5–x[Q[T53flC»flh[QOOQ cè¸hfl8HFb[QOO´[tC”[n84flèdhdb5«d[j”4M™[Bê´O[o5bEèfl,[c8fl4é5™[t»”b»[cé»fl»™[c»é86èfl”8»[I´O´B™[fb»bdmu”8dM[7èCd[–flè8bd[fl5’d5flD5’dM c5 ¸flè–C8b èC –è4C«5”b 5db ¸flèb5’H5’ ¸»fl C” 4è¸hfl8HFb 5b –8dbfl83C5’ »D54 –5d é845”45d PC8 5” fl5dbfl58H”5”b éUCb8é8d»b8è”™ é» 4è¸85™ é» –8dbfl83Cb8è”™ 5b é» –5’4諸8é»b8è”M -C4C”5 ¸»flb85 –5 45 ¸flè–C8b[èC[–è4C«5”b[”5[¸5Cb[5îbfl5[fl5¸flè–C8b5[dèCd[»C4C”5[6èfl«5™[¸»fl[PC5éPC5[«èh5”[PC5[45[dè8b™[d»”d[éU»Cbèfl8d»b8è”[¸fl5’»é»3é5[5b[5’4fl8b5 –5[tC” 5b[–5[d5d[3»8éé5Cfld[–5[é845”45™[dU8é[h[5”[»M A5[éèH8485é[–5’b5”C[¸»fl[–5d[b85fld™[5b[PC8[4諸fl5”–[é»[b54F”èéèH85[fl5é»b8D5[»CJ[¸èé845d[–5[4»fl»4b5Sfl5d™[5db[¸flèb5’H5’[¸»fl[C”[4è¸hfl8HFb[5b[é845”485’[¸»fl[–5d[6èCfl”8dd5Cfld[–5[tC”M tC”™ tC” n84flèdhdb5«d™ bF5 tC” AèHè™ k»D»™ k»D»`5»”d™ k»D»lt™ ke`c™ ke1™ kvn™ kQtf™ k8”8™ 5b tèé»fl8d dè”b –5d «»flPC5d –5 6»3fl8PC5 èC –5d «»flPC5d –5’¸èd5’5d –5 tC” n84flèdhdb5«d™ j”4M »CJ[fb»bdmu”8d[5b[–»”d[–U»Cbfl5d[¸»hdM uojy[5db[C”5[«»flPC5d[–5’¸èd5’5[»CJ[fb»bdmu”8d[5b[–»”d[–U»Cbfl5d[¸»hd[5b[é845”485’5[5J4éCd8D5«5”b[¸»fl[yNl¸5”[c諸»”h™[Ab–M o5bd4»¸5[o»D8H»bèfl[5db[C”5[«»flPC5[–5[o5bd4»¸5[cè««C”84»b8è”d[cèfl¸èfl»b8è”[»CJ[fb»bdmu”8d[5b[–»”d[–U»Cbfl5d[¸»hdM AU8”b5fl6»45d –UCb8é8d»b8è” Hfl»¸F8PC5 lfifo All1 5b tC”g » 5’b5’ –5’D5é踸5’5 ¸»fl tC” n84flèdhdb5«d™ j”4M ¸èCfl d5d Cb8é8d»b5Cfld 5b é845”485’dM tC” fl54è””»)îb é5d 566èflbd –5 ¸8è””85fld –5 y5flèJ ¸èCfl é»fl54F5fl4F5 5b é5 –5’D5é踸5«5”b –C 4è”45¸b –5d 8”b5fl6»45d –UCb8é8d»b8è” D8dC5éé5 èC Hfl»¸F8PC5 ¸èCfl éU8”–Cdbfl85 –5 éU8”6èfl«»b8PC5M tC” –5’b85”b C”5 é845”45 ”è” 5J4éCd8D5 –5 y5flèJ dCfl éU8”b5fl6»45 –UCb8é8d»b8è” Hfl»¸F8PC5 y5flèJ™ 45bb5 é845”45 4èCDfl»”b 5’H»é5«5”b é5d é845”485’d –5 tC” PC8 «5bb5”b 5” ¸é»45 éU8”b5fl6»45 –UCb8é8d»b8è” Hfl»¸F8PC5 lfifo All1 5b PC8 5” èCbfl5 d5 4è”6èfl«5”b[»CJ[é845”45d[5’4fl8b5d[–5[tC”M AU»44èfl–[–C[HèCD5fl”5«5”b[»«5’fl84»8”[5db[fl5PC8d[»D»”b[éU5J¸èflb»b8è”[–C[¸flè–C8bM A- elcunfo7-7jlo ft7 Tlusojf 9fo AUf7-7; f7 7lu7ft -u7sft cloej7jlot™ efcA-s-7jlot f7 i-s-o7jft fyfisfttft lu 7-cj7ft tlo7 TlsnfAAfnfo7 fycAuft™ e-ot A- nftusf -u7lsjtff fi-s A- Alj -fifiAjc-`Af™ clnfisjt ol7-nnfo7 7lu7f i-s-o7jf jnfiAjcj7f sfA-7jvf - A- ru-Aj7f n-sc“-oef™ - AU-fi7j7uef[-[uof[u7jAjt-7jlo[fi-s7jcuAjfsf[lu -[AU-`tfocf[ef[clo7sfT-ca loM Course Contents About This Course..................................................................................... Preface-xvi cèCfld5[iè»éd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMfifl56»45mJD88 cèCfld5[lD5flD85E MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM fifl56»45mJ8J cèCfld5[n»¸ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM fifl56»45mJJ 7è¸84d[oèb[cèD5fl5– MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM fifl56»45mJJ8 “èE[fifl5¸»fl5–[-fl5[ èCK MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMfifl56»45mJJ88 j”bflè–C4b8è”d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMfifl56»45mJJ888 “èE[bè[ud5[bF5[j4è”d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMfifl56»45mJJ8D 7h¸èHfl»¸F84»é[cè”D5”b8è”d[»”–[th«3èéd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMfifl56»45mJJD Getting Started......................................................................................................... 1-1 l3+54b8D5d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmQ s5é5D»”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmR wF»b[jd[bF5[k»D»g[754F”èéèHhK MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmB fifl8«»flh[iè»éd[è6[bF5[k»D»[754F”èéèHh MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM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MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmêˆ -[t8«¸é5[k»D»[-¸¸é84»b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmêı 7F5 TestGreeting[-¸¸é84»b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmêÇ 7F5 Greeting[cé»dd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmêI c諸8é8”H[»”–[sC””8”H[bF5 TestGreeting[fiflèHfl»« MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmQO Java™ Programming Language iv 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services c諸8é5m78«5[fflflèfld MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmQê sC”b8«5[fflflèfld MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmQQ k»D» 754F”èéèHh sC”b8«5 f”D8fl蔫5”b MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM êmQR Object-Oriented Programming................................................................................ 2-1 l3+54b8D5d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmQ s5é5D»”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmR tè6bE»fl5[f”H8”55fl8”H MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmB 7F5[-”»éhd8d[»”–[e5d8H”[fiF»d5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Qm´ -3dbfl»4b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Qmˆ cé»dd5d[»d[`éC5¸fl8”bd[6èfl[l3+54bd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Qmı e54é»fl8”H[k»D»[754F”èéèHh[cé»dd5d MMMMMMMMMMMMMMM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MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmQR 75fl«8”èéèHh[s54»¸ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmQB ud8”H[bF5[k»D»[754F”èéèHh[-fij eè4C«5”b»b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmQ´ k»D»[754F”èéèHh[-fij[eè4C«5”b»b8è”[w8bF[“7nAR MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM QmQˆ Java™ Programming Language v Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Identifiers, Keywords, and Types........................................................................... 3-1 l3+54b8D5d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQ s5é5D»”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmB cè««5”bd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM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MMMMMMMMMMMMMM RmêB j”b5Hfl»é[2 byte™ short™ int™[»”– long MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Rmê´ Téè»b8”H[fiè8”b[2 float[»”– double MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Rmêı v»fl8»3é5d™[e54é»fl»b8è”d™[»”–[-dd8H”«5”bd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmêI k»D»[s565fl5”45[7h¸5d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQO cè”dbflC4b8”H[»”–[j”8b8»é8G8”H[l3+54bd MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQê n5«èflh[-ééè4»b8è”[»”–[A»hèCb MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQQ fJ¸é848b[-bbfl83Cb5[j”8b8»é8G»b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQR fJ54Cb8”H[bF5[cè”dbflC4bèfl MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQB -dd8H”8”H[»[v»fl8»3é5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQ´ -dd8H”8”H[s565fl5”45d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQˆ fi»ddm3hmv»éC5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmQı 7F5 this[s565fl5”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmRQ k»D»[fiflèHfl»««8”H[A»”HC»H5[cè–8”H[cè”D5”b8è”d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM RmRˆ Expressions and Flow Control............................................................................... 4-1 l3+54b8D5d MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmQ s5é5D»”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmB v»fl8»3é5d[»”–[t4è¸5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Bm´ v»fl8»3é5[t4è¸5[fJ»«¸é5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Bmˆ Java™ Programming Language vi Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services v»fl8»3é5[j”8b8»é8G»b8è” MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Bmı j”8b8»é8G»b8è”[`56èfl5[ud5[fifl8”48¸é5 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmÇ l¸5fl»bèfl fifl545–5”45 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmI AèH84»é[l¸5fl»bèfld MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmêO `8bE8d5[AèH84»é[l¸5fl»bèfld MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Bmêê s8HFbmtF86b[l¸5fl»bèfld >>[»”– >>> MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM BmêQ A56bmtF86b[l¸5fl»bèfl 0 ) { 8 weight = newWeight; 9 } 10 } 11 } Java™ Programming Language Module 2, slide 10 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Accessing Object Members Y 7F5 wbi[”èb»b8è”[8dx. Y 7F8d 8d[Cd5–[bè[»445dd[è3+54b[«5«35fld™[8”4éC–8”H »bbfl83Cb5d[»”–[«5bFè–dM Y fJ»«¸é5d[è6[–èb[”èb»b8è”[»fl5x d.setWeight(42); d.weight = 42; // only permissible if weight is public Java™ Programming Language Module 2, slide 11 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Information Hiding 7F5[¸flè3é5«x MyDate cé85”b[4è–5[F»d[–8fl54b[»445dd[bè +day : int 8”b5fl”»é[–»b»[Vd[fl565fld[bè[» MyDate +month : int +year : int è3+54bWx d.day = 32; // invalid day d.month = 2; d.day = 30; // plausible but wrong d.day = d.day + 1; // no check for wrap around Java™ Programming Language Module 2, slide 12 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Information Hiding 7F5[dèéCb8è”x MyDate cé85”b[4è–5[«Cdb[Cd5[d5bb5fld[»”– -day : int H5bb5fld[bè[»445dd[8”b5fl”»é[–»b»x -month : int -year : int MyDate d = new MyDate(); +getDay() : int +getMonth() : int d.setDay(32); +getYear() : int +setDay(int) : boolean // invalid day, returns false +setMonth(int) : boolean +setYear(int) : boolean d.setMonth(2); d.setDay(30); // plausible but wrong, // setDay returns false Verify days in month d.setDay(d.getDay() + 1); // this will return false if wrap around // needs to occur Java™ Programming Language Module 2, slide 13 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Encapsulation Y “8–5d[bF5[8«¸é5«5”b»b8è”[–5b»8éd[è6[»[4é»dd Y Tèfl45d[bF5[Cd5fl[bè[Cd5 »”[8”b5fl6»45[bè[»445dd[–»b» Y n»,5d[bF5[4è–5[«èfl5[«»8”b»8”»3é5 MyDate -date : long +getDay() : int +getMonth() : int +getYear() : int +setDay(int) : boolean +setMonth(int) : boolean +setYear(int) : boolean -isDayValid(int) : boolean Java™ Programming Language Module 2, slide 14 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Declaring Constructors Y `»d84 dh”b»J[è6[»[4è”dbflC4bèflx [] ( * ) { * } Y fJ»«¸é5x 1 public class Dog { 2 3 private int weight; 4 5 public Dog() { 6 weight = 42; 7 } 8 } Java™ Programming Language Module 2, slide 15 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The Default Constructor Y 7F5fl5[8d[»éE»hd[»b[é5»db[è”5[4è”dbflC4bèfl[8”[5D5flh[4é»ddM Y j6 bF5[Efl8b5fl[–è5d[”èb[dC¸¸éh[»”h[4è”dbflC4bèfld™[bF5 –56»Céb[4è”dbflC4bèfl[8d[¸fl5d5”b[»Cbè«»b84»ééhx Y 7F5[–56»Céb[4è”dbflC4bèfl[b»,5d ”è[»flHC«5”bd Y 7F5[–56»Céb[4è”dbflC4bèfl[3è–h[8d[5«¸bh Y 7F5 –56»Céb 5”»3é5d hèC bè 4fl5»b5 è3+54b 8”db»”45d E8bF new Xxx()E8bFèCb[F»D8”H[bè[Efl8b5[»[4è”dbflC4bèflM Java™ Programming Language Module 2, slide 16 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Source File Layout Y `»d84 dh”b»J[è6[»[k»D»[dèCfl45[68é5[8dx [] * + Y Tèfl[5J»«¸é5™[bF5 VehicleCapacityReport.java[68é5 8dx 1 package shipping.reports; 2 3 import shipping.domain.*; 4 import java.util.List; 5 import java.io.*; 6 7 public class VehicleCapacityReport { 8 private List vehicles; 9 public void generateReport(Writer output) {...} 10 } Java™ Programming Language Module 2, slide 17 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Software Packages Y fi»4,»H5d[F5é¸[«»”»H5[é»flH5[dè6bE»fl5[dhdb5«dM Y fi»4,»H5d[4»”[4è”b»8”[4é»dd5d[»”–[dC3m¸»4,»H5dM shipping gui domain Owns 0..* Company Vehicle reports Truck RiverBarge Java™ Programming Language Module 2, slide 18 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The package Statement Y `»d84 dh”b»J[è6[bF5 package db»b5«5”b[8dx package [.]*; Y fJ»«¸é5d[è6[bF5[db»b5«5”b[»fl5x package shipping.gui.reportscreens; Y t¸5486h bF5 ¸»4,»H5 –54é»fl»b8è” »b bF5 35H8””8”H è6 bF5 dèCfl45[68é5M Y l”éh[è”5[¸»4,»H5[–54é»fl»b8è”[¸5fl[dèCfl45[68é5M Y j6 ”è[¸»4,»H5[8d[–54é»fl5–™[bF5”[bF5[4é»dd[8d[¸é»45–[8”bè bF5[–56»Céb[¸»4,»H5M Y fi»4,»H5[”»«5d[«Cdb[35[F85fl»fl4F84»é[»”–[d5¸»fl»b5–[3h –èbdM Java™ Programming Language Module 2, slide 19 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The import Statement Y `»d84 dh”b»J[è6[bF5 import[db»b5«5”b[8dx import [.]*.; OR import [.]*.*; Y fJ»«¸é5d[è6[bF5[db»b5«5”b[»fl5x import java.util.List; import java.io.*; import shipping.gui.reportscreens.*; Y 7F5[8«¸èflb[db»b5«5”b[–è5d[bF5[6èééèE8”Hx Y fifl545–5d[»éé[4é»dd[–54é»fl»b8è”d Y 75ééd[bF5[4諸8é5fl[EF5fl5[bè[0”–[4é»dd5d Java™ Programming Language Module 2, slide 20 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Directory Layout and Packages Y fi»4,»H5d »fl5 dbèfl5– 8” bF5 –8fl54bèflh bfl55 4è”b»8”8”H bF5 ¸»4,»H5[”»«5M Y -”[5J»«¸é5[8d[bF5[dF8¸¸8”H[»¸¸é84»b8è”[¸»4,»H5dM shipping/ domain/ Company.class Vehicle.class RiverBarge.class Truck.class gui/ reports/ VehicleCapacityReport.class Java™ Programming Language Module 2, slide 21 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Development JavaProjects/ ShippingPrj/ src/ shipping/ domain/ gui/ reports/ docs/ classes/ shipping/ domain/ gui/ reports/ Java™ Programming Language Module 2, slide 22 of 26 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Compiling Using the -d Option cd JavaProjects/ShippingPrj/src javac -d../classes shipping/domain Java™ Programming Language Module 3, slide 5 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Semicolons, Blocks, and White Space Y - hitifi,fidi[8d[è”5[èfl[«èfl5[é8”5d[è6[4è–5[b5fl«8”»b5–[3h »[d5«84èéè”[V;Wx totals = a + b + c + d + e + f; Y -. blI 8d[»[4èéé54b8è”[è6[db»b5«5”bd[3èC”–[3h[è¸5”8”H »”–[4éèd8”H[3fl»45dx { x = y + 1; y = x + 1; } Java™ Programming Language Module 3, slide 6 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Semicolons, Blocks, and White Space Y - l thh[–568”8b8è”[Cd5d[»[d¸548»é[3éè4,x public class MyDate { private int day; private int month; private int year; } Y èC[4»”[”5db[3éè4,[db»b5«5”bdM while ( i < large ) { a = a + i; // nested block if ( a == max ) { b = b + a; a = 0; } i = i + 1; } Java™ Programming Language Module 3, slide 7 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Semicolons, Blocks, and White Space Y -”h[»«èC”b[è6 -TAififlhctlfi[8d[¸5fl«8bb5–[8”[»[k»D» ¸flèHfl»«M Tèfl 5J»«¸é5x {int x;x=23*54;} 8d 5PC8D»é5”b bèx { int x; x = 23 * 54; } Java™ Programming Language Module 3, slide 8 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Identifiers j–5”b805fld[F»D5[bF5[6èééèE8”H[4F»fl»4b5fl8db84dx Y -fl5[”»«5d H8D5”[bè[»[D»fl8»3é5™[4é»dd™[èfl[«5bFè– Y c»”[db»flb[E8bF[»[u”84è–5[é5bb5fl™[C”–5fld4èfl5 V_W™[èfl –èéé»fl[d8H”[V$W Y -fl5[4»d5md5”d8b8D5[»”–[F»D5[”è[«»J8«C«[é5”HbF Y fJ»«¸é5dx identifier userName user_name _sys_var1 $change Java™ Programming Language Module 3, slide 9 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Java Programming Language Keywords abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while s5d5flD5– é8b5fl»é[Eèfl–dx null™ true™[»”– false Java™ Programming Language Module 3, slide 10 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Primitive Types 7F5[k»D»[¸flèHfl»««8”H[é»”HC»H5[–50”5d[58HFb[¸fl8«8b8D5 bh¸5dx Y AèH84»é[2 boolean Y 75JbC»é[2 char Y j”b5Hfl»é[2 byte™ short™ int™[»”– long Y Téè»b8”H[2 double[»”– float Java™ Programming Language Module 3, slide 11 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Logical – boolean 7F5 boolean[¸fl8«8b8D5[F»d[bF5[6èééèE8”H[4F»fl»4b5fl8db84dx Y 7F5 boolean[–»b»[bh¸5[F»d[bEè[é8b5fl»éd™ true[»”– false. Y Tèfl[5J»«¸é5™[bF5[db»b5«5”bx boolean truth = true; –54é»fl5d bF5 D»fl8»3é5 truth »d boolean bh¸5 »”– »dd8H”d 8b » D»éC5 è6 true. Java™ Programming Language Module 3, slide 12 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Textual – char 7F5[b5JbC»é char[¸fl8«8b8D5[F»d[bF5 6èééèE8”H 4F»fl»4b5fl8db84dx Y s5¸fl5d5”bd[»[êˆm38b[u”84è–5[4F»fl»4b5fl Y nCdb[F»D5[8bd[é8b5fl»é[5”4éèd5–[8”[d8”Hé5[PCèb5d[V’ ’W Y ud5d[bF5[6èééèE8”H[”èb»b8è”dx 'a' 7F5[é5bb5fl a '\t' 7F5[b»3[4F»fl»4b5fl '\u????' -[d¸548684[u”84è–5[4F»fl»4b5fl™ ????™[8d[fl5¸é»45–[E8bF 5J»4béh[6èCfl[F5J»–548«»é[–8H8bd[M Tèfl[5J»«¸é5™ ’\u03A6’[8d[bF5[ifl55,[é5bb5fl[¸F8[:Φ[M Java™ Programming Language Module 3, slide 13 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Textual – String 7F5[b5JbC»é String[bh¸5[F»d[bF5 6èééèE8”H 4F»fl»4b5fl8db84dx Y jd[”èb[»[¸fl8«8b8D5[–»b»[bh¸5L[8b[8d[»[4é»dd Y “»d[8bd[é8b5fl»é 5”4éèd5–[8”[–èC3é5[PCèb5d[V" "W "The quick brown fox jumps over the lazy dog." Y c»” 35 Cd5– »d 6èééèEdx String greeting = "Good Morning !! \n"; String errorMessage = ]Record Not Found !]L Java™ Programming Language Module 3, slide 14 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Integral – byte, short, int, and long 7F5[8”b5Hfl»é[¸fl8«8b8D5d F»D5[bF5[6èééèE8”H[4F»fl»4b5fl8db84dx Y j”b5Hfl»é[¸fl8«»b5d[Cd5[bFfl55[6èfl«dx[e548«»é™[è4b»é™[èfl F5J»–548«»é 2 7F5[–548«»é[6èfl«[6èfl[bF5[8”b5H5fl 2M 077 7F5[é5»–8”H 0[8”–84»b5d[»”[è4b»é[D»éC5M 0xBAAC 7F5[é5»–8”H 0x[8”–84»b5d[»[F5J»–548«»é[D»éC5M Y A8b5fl»éd[F»D5[»[–56»Céb[bh¸5[è6 intM Y A8b5fl»éd[E8bF[bF5[dC668J L[èfl l[»fl5[è6[bh¸5 longM Java™ Programming Language Module 3, slide 15 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Integral – byte, short, int, and long Y j”b5Hfl»é[–»b»[bh¸5d[F»D5[bF5[6èééèE8”H[fl»”H5dx u7iHIH 1mH7IiT oS6H1b 12McH FS7IH Ç[38bd byte mQd[bè[Qdmê êˆ[38bd short mQ:U[bè[Q:U mê RQ[38bd int mQs:[bè[Qs: mê ˆB[38bd long mQVs[bè[QVs mê Java™ Programming Language Module 3, slide 16 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Floating Point – float and double 7F5[?è»b8”H[¸è8”b[¸fl8«8b8D5d[F»D5[bF5[6èééèE8”H 4F»fl»4b5fl8db84dx Y Téè»b8”Hm¸è8”b é8b5fl»é 8”4éC–5d 58bF5fl » –548«»é ¸è8”b èfl è”5[è6[bF5[6èééèE8”Hx Y E[èfl e[V»––[5J¸è”5”b8»é[D»éC5W Y F[èfl f[VfloatW Y D[èfl d[VdoubleW 3.14 -[d8«¸é5[?è»b8”Hm¸è8”b[D»éC5[V» doubleW 6.02E23 -[é»flH5[?è»b8”Hm¸è8”b[D»éC5 2.718F -[d8«¸é5 float[d8G5[D»éC5 123.4E+306D -[é»flH5 double[D»éC5[E8bF[fl5–C”–»”b[e Java™ Programming Language Module 3, slide 17 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Floating Point – float and double Y A8b5fl»éd[F»D5[»[–56»Céb[bh¸5[è6 doubleM Y Téè»b8”Hm¸è8”b[–»b»[bh¸5d[F»D5[bF5[6èééèE8”H[d8G5dx hBbSi1mH7IiT oS6H1b 12McH RQ[38bd float ˆB[38bd double Java™ Programming Language Module 3, slide 18 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Variables, Declarations, and Assignments 1 public class Assign { 2 public static void main (String args []) { 3 // declare integer variables 4 int x, y; 5 // declare and assign floating point 6 float z = 3.414f; 7 // declare and assign double 8 double w = 3.1415; 9 // declare and assign boolean 10 boolean truth = true; 11 // declare character variable 12 char c; 13 // declare String variable 14 String str; 15 // declare and assign String variable 16 String str1 = "bye"; 17 // assign value to char variable 18 c = 'A'; 19 // assign value to String variable 20 str = "Hi out there!"; 21 // assign values to int variables 22 x = 6; 23 y = 1000; 24 } 25 } Java™ Programming Language Module 3, slide 19 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Java Reference Types Y j”[k»D»[b54F”èéèHh™[35hè”–[¸fl8«8b8D5[bh¸5d[»éé[èbF5fld »fl5[fl565fl5”45[bh¸5dM Y - firfi fidlfiflJt At. fi[4è”b»8”d[» Ttdw fi[bè[»”[è3+54bM Y Tèfl[5J»«¸é5x 1 public class MyDate { 2 private int day = 1; 3 private int month = 1; 4 private int year = 2000; 5 public MyDate(int day, int month, int year) {... } 6 public String toString() {... } 7 } 1 public class TestMyDate { 2 public static void main(String[] args) { 3 MyDate today = new MyDate(22, 7, 1964); 4 } 5 } Java™ Programming Language Module 3, slide 20 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Constructing and Initializing Objects Y c»éé8”H new Xyz() ¸5fl6èfl«d[bF5[6èééèE8”H[»4b8è”dx »M n5«èflh 8d »ééè4»b5– 6èfl bF5 è3+54bM 3M fJ¸é848b »bbfl83Cb5 8”8b8»é8G»b8è” 8d ¸5fl6èfl«5–M 4M - 4è”dbflC4bèfl 8d 5J54Cb5–M –M 7F5 è3+54b fl565fl5”45 8d fl5bCfl”5– 3h bF5 new è¸5fl»bèflM Y 7F5[fl565fl5”45[bè[bF5[è3+54b[8d[»dd8H”5–[bè[»[D»fl8»3é5M Y -”[5J»«¸é5[8dx MyDate my_birth = new MyDate(22, 7, 1964); Java™ Programming Language Module 3, slide 21 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Memory Allocation and Layout Y -[–54é»fl»b8è”[»ééè4»b5d[dbèfl»H5[è”éh[6èfl[»[fl565fl5”45x MyDate my_birth = new MyDate(22, 7, 1964); my_birth ???? Y ud5[bF5 new[è¸5fl»bèfl[bè[»ééè4»b5[d¸»45[6èfl MyDatex MyDate my_birth = new MyDate(22, 7, 1964); my_birth ???? day 0 month 0 year 0 Java™ Programming Language Module 3, slide 22 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Explicit Attribute Initialization Y j”8b8»é8G5[bF5[»bbfl83Cb5d[»d[6èééèEdx MyDate my_birth = new MyDate(22, 7, 1964); my_birth ???? day 1 month 1 year 2000 Y 7F5[–56»Céb[D»éC5d[»fl5[b»,5”[6flè«[bF5[»bbfl83Cb5 –54é»fl»b8è”[8”[bF5[4é»ddM Java™ Programming Language Module 3, slide 23 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Executing the Constructor Y fJ54Cb5[bF5[«»b4F8”H[4è”dbflC4bèfl »d[6èééèEdx MyDate my_birth = new MyDate(22, 7, 1964); my_birth ???? day 22 month 7 year 1964 Y j”[bF5[4»d5[è6[»”[èD5fléè»–5–[4è”dbflC4bèfl™[bF5[68fldb 4è”dbflC4bèfl[4»”[4»éé[»”èbF5flM Java™ Programming Language Module 3, slide 24 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Assigning a Variable Y -dd8H”[bF5[”5Eéh[4fl5»b5–[è3+54b[bè[bF5[fl565fl5”45 D»fl8»3é5[»d[6èééèEdx MyDate my_birth = new MyDate(22, 7, 1964); my_birth 0x01abcdef day 22 month 7 year 1964 Java™ Programming Language Module 3, slide 25 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Assigning References Y 7Eè[D»fl8»3é5d[fl565fl[bè[»[d8”Hé5[è3+54bx 1 int x = 7; 2 int y = x; 3 MyDate s = new MyDate(22, 7, 1964); 4 MyDate t = s; x 7 22 7 1964 y 7 s 0x01234567 t 0x01234567 Y s5»dd8H”«5”b[«»,5d[bEè[D»fl8»3é5d ¸è8”b[bè[bEè è3+54bdx 5 t = new MyDate(22, 12, 1964); x 7 22 7 1964 y 7 s 0x01234567 22 12 1964 t 0x12345678 Java™ Programming Language Module 3, slide 26 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Pass-by-Value Y j”[»[d8”Hé5[D8flbC»é[«»4F8”5™[bF5[k»D»[¸flèHfl»««8”H é»”HC»H5[è”éh[¸»dd5d[»flHC«5”bd[3h[D»éC5M Y wF5”[»”[è3+54b[8”db»”45[8d ¸»dd5–[»d[»”[»flHC«5”b[bè[» «5bFè–™[bF5[D»éC5[è6[bF5[»flHC«5”b[8d[» firfi fidlfi[bè[bF5 è3+54bM Y 7F5 lbdifidih[è6[bF5[è3+54b[4»”[35[4F»”H5–[8”[bF5[4»éé5– «5bFè–™[3Cb[bF5[èfl8H8”»é[è3+54b[fl565fl5”45[8d[”5D5fl 4F»”H5–M Java™ Programming Language Module 3, slide 27 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Pass-by-Value 1 public class PassTest { 2 3 // Methods to change the current values 4 public static void changeInt(int value) { 5 value = 55; 6 } 7 public static void changeObjectRef(MyDate ref) { 8 ref = new MyDate(1, 1, 2000); 9 } 10 public static void changeObjectAttr(MyDate ref){ 11 ref.setDay(4); 12 } Java™ Programming Language Module 3, slide 28 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Pass-by-Value 13 14 public static void main(String args[]) { 15 MyDate date; 16 int val; 17 18 // Assign the int 19 val = 11; 20 // Try to change it 21 changeInt(val); 22 // What is the current value? 23 System.out.println("Int value is: " + val); 7F5[fl5dCéb[è6[bF8d[èCb¸Cb[8dx Int value is: 11 Java™ Programming Language Module 3, slide 29 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Pass-by-Value 24 25 // Assign the date 26 date = new MyDate(22, 7, 1964); 27 // Try to change it 28 changeObjectRef(date); 29 // What is the current value? 30 System.out.println("MyDate: " + date); 7F5 fl5dCéb è6 bF8d èCb¸Cb 8dx MyDate: 22-7-1964 Java™ Programming Language Module 3, slide 30 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Pass-by-Value 31 32 // Now change the day attribute 33 // through the object reference 34 changeObjectAttr(date); 35 // What is the current value? 36 System.out.println("MyDate: " + date); 37 } 38 } 7F5[fl5dCéb[è6[bF8d[èCb¸Cb[8dx MyDate: 4-7-1964 Java™ Programming Language Module 3, slide 31 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The this Reference “5fl5[»fl5[»[65E[Cd5d[è6[bF5 this[,5hEèfl–x Y 7è[fl5dèéD5[»«38HC8bh[35bE55”[8”db»”45[D»fl8»3é5d[»”– ¸»fl»«5b5fld Y 7è[¸»dd[bF5[4Cflfl5”b[è3+54b[»d[»[¸»fl»«5b5fl[bè[»”èbF5fl «5bFè–[èfl[4è”dbflC4bèfl Java™ Programming Language Module 3, slide 32 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The this Reference 1 public class MyDate { 2 private int day = 1; 3 private int month = 1; 4 private int year = 2000; 5 6 public MyDate(int day, int month, int year) { 7 this.day = day; 8 this.month = month; 9 this.year = year; 10 } 11 public MyDate(MyDate date) { 12 this.day = date.day; 13 this.month = date.month; 14 this.year = date.year; 15 } Java™ Programming Language Module 3, slide 33 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The this Reference 16 17 public MyDate addDays(int moreDays) { 18 MyDate newDate = new MyDate(this); 19 newDate.day = newDate.day + moreDays; 20 // Not Yet Implemented: wrap around code... 21 return newDate; 22 } 23 public String toString() { 24 return "" + day + "-" + month + "-" + year; 25 } 26 } Java™ Programming Language Module 3, slide 34 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services The this Reference 1 public class TestMyDate { 2 public static void main(String[] args) { 3 MyDate my_birth = new MyDate(22, 7, 1964); 4 MyDate the_next_week = my_birth.addDays(7); 5 6 System.out.println(the_next_week); 7 } 8 } Java™ Programming Language Module 3, slide 35 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Java Programming Language Coding Conventions Y fi»4,»H5dx com.example.domain; Y cé»dd5d™[8”b5fl6»45d™[»”– enum[bh¸5dx SavingsAccount Y n5bFè–dx getAccount() Y v»fl8»3é5dx currentCustomer Y cè”db»”bdx HEAD_COUNT Java™ Programming Language Module 3, slide 36 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Java Programming Language Coding Conventions Y cè”bflèé dbflC4bCfl5dx if ( condition ) { statement1; } else { statement2; } Y t¸»48”Hx Y ud5[è”5[db»b5«5”b[¸5fl[é8”5M Y ud5[bEè[èfl[6èCfl d¸»45d[6èfl[8”–5”b»b8è”M Y cè««5”bdx Y ud5 //[bè[4è««5”b[8”é8”5[4è–5M Y ud5 [6èfl[4é»dd[«5«35fldM Java™ Programming Language Module 3, slide 37 of 37 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services nbW4BH1a Expressions and Flow Control Java™ Programming Language Sun Services Objectives Y e8db8”HC8dF[35bE55”[8”db»”45[»”–[éè4»é[D»fl8»3é5d Y e5d4fl835[FèE[bè[8”8b8»é8G5[8”db»”45[D»fl8»3é5d Y j–5”b86h[»”–[4èflfl54b[» Possible reference before assignment 4諸8é5fl[5flflèfl Y s54èH”8G5™[–5d4fl835™[»”–[Cd5 k»D»[dè6bE»fl5[è¸5fl»bèfld Y e8db8”HC8dF[35bE55”[é5H»é[»”–[8éé5H»é[»dd8H”«5”bd[è6 ¸fl8«8b8D5[bh¸5d Java™ Programming Language Module 4, slide 2 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Objectives Y j–5”b86h boolean[5J¸fl5dd8è”d[»”–[bF58fl[fl5PC8fl5«5”bd 8”[4è”bflèé[4è”dbflC4bd Y s54èH”8G5 »dd8H”«5”b 4諸»b838é8bh »”– fl5PC8fl5– 4»dbd 8”[6C”–»«5”b»é[bh¸5d Y ud5 if™ switch™ for™ while™ »”– do 4è”dbflC4b8è”d »”– bF5[é»35é5–[6èfl«d[è6 break[»”– continue[»d[6éèE 4è”bflèé[dbflC4bCfl5d[8”[»[¸flèHfl»« Java™ Programming Language Module 4, slide 3 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Relevance Y wF»b[bh¸5d[è6[D»fl8»3é5d[»fl5[Cd56Cé[bè ¸flèHfl»««5fldK Y c»”[«Céb8¸é5[4é»dd5d[F»D5[D»fl8»3é5d[E8bF[bF5[d»«5 ”»«5[»”–™[86[dè™[EF»b[8d[bF58fl[d4è¸5K Y wF»b[bh¸5d[è6[4è”bflèé[dbflC4bCfl5d[»fl5[Cd5–[8”[èbF5fl é»”HC»H5dK[wF»b[«5bFè–d[–è[bF5d5[é»”HC»H5d[Cd5[bè 4è”bflèé 6éèEK Java™ Programming Language Module 4, slide 4 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Variables and Scope Aè4»é[D»fl8»3é5d »fl5x Y v»fl8»3é5d[bF»b[»fl5[–568”5–[8”d8–5[»[«5bFè–[»”–[»fl5 4»éé5– blt ™ tjib,tiAl™ ifi,cb t nv[èfl hitlI[D»fl8»3é5d Y v»fl8»3é5d bF»b »fl5 4fl5»b5– EF5” bF5 «5bFè– 8d 5J54Cb5– »fl5[–5dbflèh5–[EF5”[bF5[«5bFè–[8d[5J8b5– v»fl8»3é5[8”8b8»é8G»b8è”[4諸fl8d5d[bF5[6èééèE8”Hx Y Aè4»é[D»fl8»3é5d[fl5PC8fl5[5J¸é848b[8”8b8»é8G»b8è”M Y j”db»”45[D»fl8»3é5d[»fl5[8”8b8»é8G5–[»Cbè«»b84»ééhM Java™ Programming Language Module 4, slide 5 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Variable Scope Example public class ScopeExample { private int i=1; fpHl4iAb71tiSl5 public void firstMethod() { int i=4, j=5; fiHSc1nH6b M this.i = i + j; j 8 secondMethod(7); } secondMethod i 7 public void secondMethod(int i) { this int j=8; ScopeExample j 5 this.i = i + j; i 1 } firstMethod i 4 } this main scope public class TestScoping { public static void main(String[] args) { ScopeExample scope = new ScopeExample(); scope.firstMethod(); } } Java™ Programming Language Module 4, slide 6 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Variable Initialization vS ASyBH vSB4H byte 0 short 0 int 0 long 0L float 0.0F double 0.0D char '\u0000' boolean false -éé fl565fl5”45[bh¸5d null Java™ Programming Language Module 4, slide 7 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Initialization Before Use Principle 7F5[4諸8é5fl[E8éé[D5fl86h bF»b[éè4»é[D»fl8»3é5d[F»D5[355” 8”8b8»é8G5–[356èfl5[Cd5–M 3 public void doComputation() { 4 int x = (int)(Math.random() * 100); 5 int y; 6 int z; 7 if (x > 50) { 8 y = 9; 9 } 10 z = y + x; // Possible use before initialization 11 } javac TestInitBeforeUse.java TestInitBeforeUse.java:10: variable y might not have been initialized z = y + x; // Possible use before initialization ^ 1 error Java™ Programming Language Module 4, slide 8 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Operator Precedence xcH Sib 3 blASiAkH ++ -- + jdt n - jdt n ~ ! () s bè A * / % A[bè[s + - A[bè[s > >>> A[bè[s < > = instanceof A[bè[s == != A[bè[s & A[bè[s ^ A[bè[s | A[bè[s && A[bè[s || A[bè[s ? : s[bè[A = *= /= %= += -= = >>>= &= ^= |= s bè A Java™ Programming Language Module 4, slide 9 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Logical Operators Y 7F5 boolean[è¸5fl»bèfld[»fl5x ! – NOT & – AND | – OR ^ – XOR Y 7F5[dFèflbm48fl4C8b boolean[è¸5fl»bèfld[»fl5x && – AND || – OR Y èC 4»” Cd5 bF5d5 è¸5fl»bèfld »d 6èééèEdx MyDate d = reservation.getDepartureDate(); if ( (d != null) && (d.day > 31) { // do something with d } Java™ Programming Language Module 4, slide 10 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Bitwise Logical Operators Y 7F5[8”b5H5fl.Ai-Ahfi[è¸5fl»bèfld[»fl5x ~ – Complement & – AND ^ – XOR | – OR Y `hb5md8G5– 5J»«¸é5d[8”4éC–5x 0 0 1 0 1 1 0 1 ~ 0 1 0 0 1 1 1 1 & 0 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 ^ 0 1 0 0 1 1 1 1 | 0 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1 Java™ Programming Language Module 4, slide 11 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Right-Shift Operators >> and >>> Y AiT,fiiAl[èfl hAudfiw[fl8HFb[dF86b[V>>W[è¸5fl»bèflx Y fJ»«¸é5d[»fl5x 128 >> 1 returns 128/2: = 64 256 >> 4 returns 256/2a = 16 -256 >> 4 returns -256/2a = -16 Y 7F5[d8H”[38b[8d[4è¸85–[–Cfl8”H[bF5[dF86bM Y mbuAlt [èfl jdhAudfiwfl AuTiLhTAri[V>>>W[è¸5fl»bèflx Y 7F8d è¸5fl»bèfl 8d Cd5– 6èfl 38b ¸»bb5fl”dM Y 7F5[d8H”[38b[8d[”èb[4è¸85–[–Cfl8”H[bF5[dF86bM Java™ Programming Language Module 4, slide 12 of 31 Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F Sun Services Left-Shift Operator >> 5 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 -1357 >>> 5 = 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1357