intermediate java programming language (in thai)

322
Java 101: Intermediate Java Programming Language หลกสตร Mini Master of Java Technology ผศ.ธนศา เครอไวศยวรรณ คณะเทคโนโลยสารสนเทศ สถาบนเทคโนโลยพระจอมเกล!าเจ!าค"ณทหารลาดกระบง ผศ. ธนศา เครอไวศยวรรณ Java Programming i

Upload: thanachart-numnonda

Post on 18-Dec-2014

1.642 views

Category:

Technology


9 download

DESCRIPTION

เอกสารการเขียนโปรแกรมภาษาจาวา หลักสูตร Mini Master of Java Technology ฉบับปรับปรุง

TRANSCRIPT

  • Java 101: Intermediate JavaProgramming Language

    Mini Master of Java Technology

    .

    !!"

    . Java Programming i

  • 1 .......................................................................................................................................1

    1.1 ###.................................................................................................................................11.2 #.................................................................................................................................................... 21.3 ) ............................................................................................................................................................ 61.4 )5# ......................................................................................................................71.5 )..................................................................................................................................................... 91.6 ;...................................................................................................................................................... 10

    1.6.1 Java Virtual Machine....................................................................................................................................111.6.2 Java Runtime Environment...........................................................................................................................121.6.3 Java Development Kit...................................................................................................................................13

    1.7 JL............................................................................................................................................. 151.8 #;...........................................................................................................................16

    1.8.1 Java Platform, Standard Edition...................................................................................................................181.8.2 Java Platform, Enterprise Edition.................................................................................................................181.8.3 Java Platform, Micro Edition........................................................................................................................19

    1.9 ............................................................................................................................................... 201.9.1 UJ#..................................................................................................................201.9.2 U5................................................................................................................22

    1.10 VL Java API...................................................................................................................................................... 24JY....................................................................................................................................................... 25

    2 .................................................................................................................................272.1 ;5#..........................................................................................................272.2 #)Z........................................................................................................................................... 29

    2.2.1 5# ...................................................................................................................................................... 292.2.2 Identifier........................................................................................................................................................ 302.2.3 ;##........................................................................................................................................................... 31

    ii Java Programming .

  • 2.2.4 ))#Z........................................................................................................................................... 322.2.5 LL ......................................................................................................................................................... 332.2.6 UVL;\.................................................................................................................................................. 33

    2.3 UVY......................................................................................................................................... 332.3.1 UV.......................................................................................................................................... 352.3.2 UV)).....................................................................................................................................352.3.3 UV)Z5 ........................................................................................................................362.3.4 UV)..............................................................................................................................37

    2.4 )L;\................................................................................................................................................. 382.4.1 Z)\ZL .............................................................................................................................................392.4.2 L;\............................................................................................................................................................ 412.4.3 )L;\....................................................................................................................42

    2.5 )Z ......................................................................................................................................................... 452.5.1 )Z#.......................................................................................................................462.5.2 )Z))#............................................................................................................................482.5.3 )Z#......................................................................................................................502.5.4 )Z....................................................................................................................................522.5.5 Z)Z))Z..............................................................................................................53

    2.6 UV.............................................................................................................................................. 542.6.1 UVZ)\ZL...............................................................................................................552.6.2 Typecasting................................................................................................................................................... 57

    2.7 UVU........................................................................................................................................... 582.7.1 String................................................................................................................................................... 602.7.2 Math................................................................................................................................................... 62

    2.8 Z)\J#J........................................................................................................................................ 642.8.1 System.out.println()...................................................................................................................................... 642.8.2 )UVU Command Line.......................................................................................................65

    JY....................................................................................................................................................... 66 3 %................................................................................................................................................. 69

    . Java Programming iii

  • 3.1 Z)\UJ......................................................................................................................................... 693.2 UZ..........................................................................................................................................70

    3.2.1 Z)\ if........................................................................................................................................................... 703.2.2 Z)\ if..else.................................................................................................................................................. 723.2.3 Z)\ if U..........................................................................................................................................743.2.4 Z)\ switch.................................................................................................................................................. 78

    3.3 UZYZ .............................................................................................................................................803.3.1 Z)\ while.................................................................................................................................................... 813.3.2 Z)\ do..while.............................................................................................................................................. 833.3.3 Z)\ for........................................................................................................................................................ 84

    3.4 UU (Nested Structure)................................................................................................................863.4.1 Z)\ break, continue label ...............................................................................................................88

    JY....................................................................................................................................................... 90

    4 ,-.............................................................................................................................................934.1 #5#.............................................................................................................93

    4.1.1 5#....................................................................................................................................................... 934.1.2 .............................................................................................................................................................. 944.1.3 J) ................................................................................................................................................... 954.1.4 ........................................................................................................................................................... 97

    4.2 ;5#U...............................................................................................984.2.1 ..........................................................................................................................................984.2.2 J)................................................................................................................................994.2.3 ........................................................................................................................................ 994.2.4 U5#..................................................................................................................1014.2.5 ;U5#................................................................................................................102

    4.3 J)L5#........................................................................................................1044.3.1 LJU ................................................................................................................................................... 1044.3.2 ................................................................................................................................................ 106

    iv Java Programming .

  • 4.3.3 ;UV..................................................................................................................................1074.4 ;5#\U abstract #................................................109

    4.4.1 abstract......................................................................................................................................1094.4.2 #................................................................................................................................................. 111

    4.5 5................................................................................................................................................................ 1134.5.1 U5........................................................................................................................114

    4.6Unified Modeling Language................................................................................................................................1154.6.1 .................................................................................................................................1164.6.2 5#............................................................................................................................116

    4.7 )Y)...............................................................................................................................117JY..................................................................................................................................................... 119

    5 003............................................................................................................1215.1 Java Foundation Class ........................................................................................................................................ 121

    5.1.1 5 AWT............................................................................................................................................. 1225.1.2 5 Swing............................................................................................................................................ 124

    5.2 Container ....................................................................................................................................1255.2.1 Frame................................................................................................................................................ 1255.2.2 Panel.................................................................................................................................................. 1275.2.3 Dialog................................................................................................................................................ 1285.2.4 JFrame............................................................................................................................................... 128

    5.3 ))Lm...............................................................................................................1315.3.1 BorderLayout.............................................................................................................................................. 1325.3.2 FlowLayout................................................................................................................................................. 1355.3.3 GridLayout.................................................................................................................................................. 137

    5.4 Lm5 Swing..............................................................................................................1385.4.1 JButton............................................................................................................................................. 1405.4.2 JLabel............................................................................................................................................... 1415.4.3 JTextField........................................................................................................................................ 144

    . Java Programming v

  • 5.4.4 JTextArea.........................................................................................................................................1455.4.5 JcheckBox JRadioButton........................................................................................................1475.4.6 JComboBox..................................................................................................................................... 1505.4.7 JList.................................................................................................................................................. 151

    5.5 UV........................................................................................................................................................ 1535.5.1 U JMenuBar...................................................................................................................................1545.5.2 U JMenu......................................................................................................................................... 1545.5.3 U JMenuItem..................................................................................................................................1555.5.4 JCheckBoxMenuItem......................................................................................................................1575.5.5 UVL......................................................................................................................................... 159

    5.6 J) Component......................................................................................................................161JY..................................................................................................................................................... 163

    6 ,-.........................................................................................................1656.1 ................................................................................................................................................................. 165

    6.1.1 ;U........................................................................................................................................1656.1.2 LL argument ..................................................................................................................................1666.1.3 )L;\L)..................................................................................................................................1726.1.4 modifier .................................................................................................................................... 172

    6.2 ;U)LJU.............................................................................................1736.2.1 Accessor..................................................................................................................................1756.2.2 ;## this.................................................................................................................................................. 178

    6.3 ;U)...........................................................................................1796.3.1 ;\VU...................................................................................................................................1826.3.2 ;## protected ...................................................................................................................................... 1836.3.3 ;\\ Object........................................................................................................................................ 1846.3.4 ;## super .............................................................................................................................................. 185

    6.4 ;UV.......................................................................................................................................... 1866.4.1 Dynamic Binding........................................................................................................................................ 186

    vi Java Programming .

  • 6.4.2 ZU;;;\L).......................................................................................................1876.4.3 Virtual Method Invocation..........................................................................................................................1906.4.4 LL argument UV.............................................................................................................1906.4.5 )Z instanceof.............................................................................................................................1916.4.6 Casting 5#................................................................................................................................192

    6.5 Constructor.......................................................................................................................................................... 1946.5.1 ; Constructor..................................................................................................................................1956.5.2 )YZZ)\ new ..............................................................................................................1966.5.3 Constructor Overloaded.....................................................................................................................1976.5.4 this()............................................................................................................................................... 1986.5.5 super().............................................................................................................................................. 1996.5.6 )YZ Constructor............................................................................................................200

    6.6 ;\\ Object...............................................................................................................................2026.6.1 toString()......................................................................................................................................... 2026.6.2 equals()............................................................................................................................................ 203

    6.7 Wrapper....................................................................................................................................... 2046.7.1 Autoboxing.................................................................................................................................................. 205

    6.8 ;##\ ;\Z)........................................................................................................................................... 2066.8.1 J) static.................................................................................................................................2066.8.2 static.........................................................................................................................................2076.8.3 Static Initializer........................................................................................................................................... 2086.8.4 ;## final................................................................................................................................................ 209

    6.9 ......................................................................................................................................................... 2106.9.1 ;\VL.....................................................................................................................2106.9.2 ;\VL...................................................................................................................212

    6.10 Generic Types.................................................................................................................................................... 2136.10.1 Generic..................................................................................................................................214

    6.11Annotation.......................................................................................................................................................... 2156.11.1 Annotation Document...............................................................................................................................216

    . Java Programming vii

  • 6.11.2 Annotation ;\U#..............................................................................................................217JY..................................................................................................................................................... 218

    7 %.3...........................................................................................................................2217.1 J#............................................................................................................................................................. 2217.2 AWTEvent........................................................................................................................................................... 2237.2.1 ActionEvent...................................................................................................................................................... 2247.2.2 WindowEvent................................................................................................................................................... 2257.2.3 MouseEvent...................................................................................................................................................... 2257.2.4 ItemEvent.......................................................................................................................................................... 2267.2.5 Event \ ....................................................................................................................................................... 2277.3 # Listener..............................................................................................................................2277.4 ))J#.......................................................................................................................................2307.4.1 U5#..............................................................................................................2317.5 U5#....................................................................................................................2327.5.1 U5#;)..........................................................................................................2347.5.2 )wJ#J#.................................................................................................................2357.5.3 Event Adapter..........................................................................................................................2377.5.4 U anonymous .....................................................................................................................238JY..................................................................................................................................................... 239

    8 .-..........................................................................................................................................2418.1 #.................................................................................................................................................................... 2418.2 #UVY..............................................................................................................................242

    8.2.1 \)#UVY.................................................................................2428.2.2 U)#UVY...........................................................................................2438.2.3 ;U#......................................................................................................................2458.2.4 ZL\UU)# ...........................................................................................2468.2.5 UZ)\ for \U#.............................................................................................247

    viii Java Programming .

  • 8.2.6 U ArrayIndexOutOfBoundsException .......................................................................2488.3 #UV..................................................................................................................................249

    8.3.1 5L)#UV......................................................................................2518.4 #..................................................................................................................................................... 252

    8.4.1 ;\))#..................................................................................................2548.4.2 #;\;Z)#L)......................................................................................................2578.4.3 ;\;\U)#..........................................................................................................................259

    8.5 5)\........................................................................................................................................................... 2618.5.1 # Collection..............................................................................................................................2628.5.2 # Set .........................................................................................................................................2638.5.3 # List ........................................................................................................................................2648.5.4 # Map .......................................................................................................................................2668.5.5 # Iterator ..................................................................................................................................2688.5.6 Vector ............................................................................................................................................. 2718.5.7 UZ)\ for Generic......................................................................................................................272

    JY..................................................................................................................................................... 273

    9 ,...................................................................................................................................2759.1 U.......................................................................................................................................................... 2759.2 Exception............................................................................................................................................................. 2769.3 Z)\ try..catch.................................................................................................................................................... 2789.4 ))U ..........................................................................................................2799.5 5 finally........................................................................................................................................................ 2819.6 ));\L5# Exception......................................................................................2839.7 U Exception xYL.........................................................................................................2859.7.1 ;\L5# Exception.......................................................................................286JY..................................................................................................................................................... 287

    10 ,%.%...................................................................................................................................289

    . Java Programming ix

  • 10.1 Stream ............................................................................................................................................................... 28910.1.1 5 java.io..........................................................................................................................................29010.1.2 Byte Stream.......................................................................................................................29210.1.3 InputStream.....................................................................................................................................29210.1.4 OutputStream..................................................................................................................................29410.1.5 Character Stream...............................................................................................................295

    10.2 Z) Stream.........................................................................................................................................29610.3 Stream )V.........................................................................................................................298

    10.3.1 DataInputStream DataOutputStream................................................................................................30110.3.2 InputStreamReader OutputStreamWriter.........................................................................................304

    10.4 File........................................................................................................................................................... 30510.4.1 RandomAccessFile.........................................................................................................................30710.4.2ObjectInputStream ObjectOutputStream...........................................................................................308

    JY..................................................................................................................................................... 310

    x Java Programming .

  • . Java Programming xi

  • 1

    Y;YyZ \Z)#)\ )yL UL)#\ )Z \)J;\U U)YZ;#L Z)L;J#5 LUU;Z;UVL Java API

    1.1 ,.

    \#y\5# ;\UZ))UV ##)\y)V;\ 1.1 x\UL)L

    1. ## (Hardware) L;\y)\#;\UL (Central Processing Unit CPU) J#LJ J#L#J LZ J#5UV

    2. ) (Operating System) ##;\)Y\UJ#L L)##U );\UwJ);VL L Windows Vista, Linux Solaris yU )Y;Y##;);\;);\L)U

    3. J# (Application Program) ;\U)\ L Word Processor, # 5# (Web Browser) yU L;YV)U#L L # (FORTRAN) (COBOL) C# C++ yU J#ZU) ))YJ#;\Z)x\LZU )\U

    . VUYU 1

  • 1.1

    %!

    EDSAC . 1949 %()% 512 ( (words) (() 600 ()% %)1

    1.2 ,.

    #Z)\;\)) (programmer) )xY\)\U\# Z;\U #Ly

    2 VUYU .

  • 1. \ (Machine Language) y;;\\#UU \UZ)\;\yJL 01010110 x\VZ##\# )Y;Y\xYVL)L\#L\ U))L;\);\U )Y;Y\y;\Ux\LLJZ)\;\J#UUL )LZ)\\;);Y

    10110011 0001100101111010 11010001 1001010010011111 0001100101011100 11010001 10010000

    10111011 11010001 10010110

    2. ; (Assembly Language) y#;\JZ)\UZ))#;\y) L 10110011 U MOV yU ZU));UULxY Z; U; ;Uy\LxZU U;;JZ)\;\yZ) L));U;UU###x;U ))YZU;Ly;\U )L;;);Y

    MOV 0, SUMMOV NUM, ACADD SUM, AC

    STO SUM, TOT

    3. )V (High-level Language) ))\U# )V #)VUJZ)\;\)\UUL UZ)\);#)\ ))LZyUU)Z##5;\)#)VU #)V U);\ZU;\;\JZ)\Uy;\xZU \;);U #)VLU)))U5L L;\)xYZUUL

    wJ);#)VVLU L;\UU));;

    . VUYU 3

  • L;\ #J;\U;L # (FORTRAN L FORmula TRANslator) )) IBM L}

    .. 1954 x .. 1957 ;YUZ))J#U## ;\UUZ#;\)U wJ)#)y;\U)U##

    (COBOL L COmmon Business Oriented Language) )xY} .. 1959 y;\)xY\U)J#UJU wJ);\UUJZL) )y;\)

    (BASIC L Beginners All-purpose Symbolic Instructional Code) y;\)xY;J#\UVU\U);VUU)LL y #;\U\#

    %!

    %% punch card %() % punch card )%%6 .. 2529 )%%%

    #;\)xYJ );UZ);\)L )Y;Y\#L)YU;\; ZU);\;L;)UyU LU} .. 1970 x;#;\y (Procedural Structural Language) xY #;Y;JL) ZUUZJ)UL \VyL #;\y;\Z)

    4 VUYU .

  • (Pascal) ) Nicklaus Wirth } .. 1971 ;J#\U; ) L\L;J);\)J)UJJxLU))

    ; (C) )xYL;) ));\U) AT&T Bell x\UZJL BCPL B UU\J)UV\UU ;Ly#;\Z)))V# (Unix) x\y #;\UJ#;\ZU5 \;)#\

    JU) ;UJ);\LUV \U;UyL ;\;LV (module) x\Uy) U;yL))YZU x\L;ZUUZJ)yU ))Yx;)#;\;L 5# (Object- Oriented Programming) ;\U)y;Z5#L )Y;Y\ULL)Z)UL (reuse) U;L #;\y5#;\Z)

    C++ y;\);\U .. 1980 ));\U) Bell U\)5#xY; ))Y)) C++ ;\))Y5# (;) U ZUwJ) C++ )y;\U)LL

    Smalltalk y5#;\)));\ Xeroxs Palo Alto Research Center (PARC) x\y#;\y5# LU L Smalltalk LU)ZU)\;) C++

    y#;\) ))))5# (Sun Microsystems) \;ZL\} .. 1995 wJ)y5#;\y;\U)x\ x\U)))JJ )y;\U\x);#U5#;U ;Y)y##m#U (Open source code)

    C# y#;\;)U);\)) Microsoft ; J#\U)))5#;\)) Windows ULxY

    . VUYU 5

  • 1.3

    #)VU) (Language Translator) \;\;xY;\;L#U (Source Code) #LUy;\;\\#UU )Ly

    1. # (Compiler) );YJZ)\#U)YUy executable code )V;\ 1.2 )LL #U; Hello.c Uy executable code ;\\ Hello.exe executable code ;\U#)VZUL5 )Y;Y\UVLV;\L)\#U )L #;\U# ; C++ # yU

    #U executable code ;\xYVL)# (##)) ))YVUULZ executable code ;\U##x\U#\ U ;;\))UZ;\)xYU#\ ))UZ#U L UU#;\U executable code Z)#;\UU)Y

    1.2

    6 VUYU .

  • 2. #;# (Interpreter) );YJZ)\#)V;Z)\Uy executable code U)\U\#Z);)V;\ 1.3 #;\U#;# ZUUL #;\U# LU;#;\U#;#UU#U )Y;Y\)YZVLL) ;Y#;#)ULL\;5 )L#;\U#;# 5 (Prolog) Smalltalk yU

    1.3

    1.4 ,-.

    ;U#)VwJ);;VLV;\Z) 5# );\LU)U;\ 1.2 ;U \U#wZ)Z LyL w#)ZL ););ULZ)Z ;w#)ZZ); Z \ yU

    . VUYU 7

  • w#)LL ;);\LLUVL) ;\;ULw#)UyL)U;\J U;\;)YZ)ZUV#;\J)YL)Y xZU)JU;\)ZU\;);\)5# )Y;Y\))YL;\UV#)YLUL;Z) U)J;U

    )5#;Uw wLU5# L x\;YUU;)J#;\J \J#\L )y5# )Y;\yV ()J) L )x ; yU ;\yL \ yU

    ;5#y#w ZJ)5# UVLV# )LL );)xLUU5#L L )x ; yU 5#)x;J)L L \ ) ;\ yU ;;\)xZUL ; \ yU

    )U#5# ZU)ZU5xY)JUUL x\))L;\U;)JUVL ;Y5#);J)L\ ;);Y

    LJU (Encapsulation) yJ);\ZU5#L)yL) x\ZUL)yL UL

    (Inheritance) yJ);\ZUZ;\)U)ULULL;

    ;UV (Polymorphism) yJ);\ZU))\LL UL

    8 VUYU .

  • 1.5 ,

    y#;\)xY ;)))5# x\\U\} .. 1991 ))5#U)JU;);\\ Green Project ;\;JJL;\#5\U)J#5#L L #5Y;; x\J#L;Y;UZ)ULZL;\;L) ))Y #;\)xYULxYVL)# (Platform Independent) ;);YZUU#L;\\L Oak x\;UZ)\U C++ )Y;Y\;)))5#;JU))V#x\)U C++ ) Oak LU;\y\ )Y;Y\;)U)L Oak y\#;\;VLU

    y#5# (Object Oriented Programming ;LL OOP) ;JL;\ZU)## ;) Green Project UU\U;\;L *7 x\y; \;;\UxY;Y\} .. 1992 ))5#UVUY;)L L5LZ5)

    %!

    James Gosling 1%) % Canada )%) Carnegie Mellon (%) Sun Microsystems

    )6 .. 1984 6 .. 2007 ) Canada Officer Order of Canada %%)

    1%%) ) Sun Microsystems %11)%9

    Duke mascot %))%) Java One Conference %)(6 9%)))%%%)%%% Duke's Choice Awards

    . VUYU 9

  • L)L #5U\;ULLxY U 5# (Web Browser) \;\;VV# HTML \#5U\#;\;#L) ))5#xU5Zy;\U;#;\U;\Z# 5U xUZ);)Yx\ U)5##;\\ Hot Java ;\) 5 (Java Applet) U\V#Z; (Proof of Technology) UZ)L Sun World95 } .. 1995

    )Y\y;\LxYL} .. 1995 \) Netscape U)5## Netscape 2.0 UZ 5 (Java Applet) UyJ)L x\)\ L IBM, Symantec, Inspire Microsoft L5)JUL L;)))5#UZJ)#) (Java Development Kit 1.0 ;\;LL JDK 1.0) L L} .. 1998 ))5#UL JDK 1.2 (LLU;\\y Java 2) U#)y# J2EE Z))J#\LL (Server) J2ME Z))\)# J2SE Z);\)\#Y } .. 2006 )5#U;\\ J2 )Yy JavaEE, JavaME JavaSE Z)

    } .. 2010 ) (Oracle Corporation) x\y##;\)UV ##J#\ UUY))5# ZUVU);Z)U;\y) x\UU;##L Z )Y;Y )5)U);;\Um m) (Open Source) Z#;\UL

    1.6

    ;U#) L)V;\ 1.4 1. Java Virtual Machine (JVM) y;;\ZU;\y#;# ;\

    #U (Java Bytecode) Uy;\\UU

    10 VUYU .

  • 2. Java Runtime Environment (JRE) y;;\U)L J# (Java Application) 5 (Java Applet) JRE U JVM Java Application Programming Interface (Java API) ;\#L ;\ZyLU

    3. Java Software Developer Kit (JDK) yJ);\U JVM ) (Java Compiler) \ (tool) \ ) API )Y x\ API yZ)\L

    1.4

    1.6.1 Java Virtual Machine\UU)J# U))

    )Y##;#U #ZU;\#U (\#.java) Uy#U (\#.class) #UL\;\)U L#UUL#;# x\#UU)\)\# #;#;Y;\;L JVM (Java Virtual Machine \)J) x\)YZU)V;\ 1.5

    JVM ZU;\#UUy\;\xYVL)##U;\U JVM ZU5L#\ ;\U#;# )Y;Y\ JVM ;JZ)\;\U;)JZ)\\L;\U ))Y JVM xZ)\#UyZ)\\L;\UULL ;Y#ULxYVL)# ))Y;\Z#U;\

    . VUYU 11

  • #)x\ );\ U ))Y; JVM VLJVM y\)J;\ZU;\)# JVM ;\Z

    ))5#;UZL L;)##L JZ)\ (instruction set) J;;# (register set) yU );UZ\ L Y;\LZ (memory area) 5 (stack), heap, garbage collection V# (class file format) yU )Y;YUZ JVM y;\m ZU)L ;\) JVM xYU LZ)VL; JVM ))5#L)Y LJ .. 2006 )-5# Um#U)##)Y

    1.5

    JVM ;\)UwJ)LLy## L5U;) JVM V##xYUUL Java Chip wJ) JVM ;U )#L 5## \UL )# \\L (Smart Card) xZUJ#L L;Y;\)UU

    1.6.2 Java Runtime EnvironmentJRE )#U;\ JVM ;)YZ )Y)V

    ;\ 1.6 1. #U )Y;YyJ;\ZyL)

    12 VUYU .

  • U Class Loader2. #U )Y;YyU Byte Code Verifier L

    #UVUUZ JVM L UL;Z)\;\)\UZUU) L UV;\ JJUVL

    3. )#U )Y;Yy)#U U Runtime Interpreter

    1.6 JRE

    1.6.3 Java Development KitJDK J)))5# x\L;L JDK )Y;Y))

    5#U;\\y Java 2 )YL JDK #) 1.2 J JDK ;\UwJ)#) 6 ( download U;\ http://java.sun.com) UL L # (javac.exe) #;# (java.exe) ;)# yU LJ JDK y;5L Software Development Kit (SDK) )Y;Y\ JDK L;;#Z);#U) ))YVUUUU SDK \L;#U

    ;Y;\VL JDK y;\U;UL command line xL

    . VUYU 13

  • U L5;U;;\ZUVU;\\U)U)YL )UU;xY LJLUU\Z)\L ;\;VLJ)\

    JDK ;\)))5#U )\ 5;) JDK xY LU#U)5# x\ JDK L;Y;Z)\;\)L;ULJ)\ L # Garbage Collection )L JDK \ ;L J9 );5 JRockit )

    %!

    NetBeans );)) 99 www.netbeans.org NetBeans 1))%) Charles %) )9))) NetBeans 6 1997 %1))96 1999 )

    % >) NetBeans ) 6.8 (% 6 .. 2010)%)) Java SE, Java ME Java EE ) SOA % C/C++, PHP Ruby

    L SDK Z));\;VLwJ);)Y;\y;# (freeware) \U (commercial software) L

    Netbeans ) Sun Microsystems (http://www.netbeans.org) Eclipse ) IBM (http://eclipse.org) JDeveloper ) Oracle (http://www.oracle.com) intelliJ IDEA ) JetBrains (http://www.jetbrains.com/idea)

    SDK L;Y;\\LUVL ULxYL \Lm

    Z)J) JDK )YZyU;;# (Editor)\U;#U

    14 VUYU .

  • x\;\U Text Editor )\UL Notepad ) Windows LU)U5xY VUUU;#;\\;#U x\L;YZU) SDK VUU;Z)\# );\)xYV;\ZUU)L ;#;Y

    EditPlus ) ES-Computing (http://www.editplus.com) JCreator ) Xinox Software (http://www.jcreator.com)

    1.7 %0

    ))5#UJxJLU);Y L (Simple) y;\LLx) )Y;Y

    ))UU C++ L \U pointer 5# (Object-Oriented) y#5#;\V#;

    J)L5# LJU ;UV (Distributed) ;JZ)\;\y5 (Package) ))

    TCP/IP ZU)5# (Distributed Object) L L#5UL

    ) (Robust) y#;\\U;\)xY;L\ ;;\xY)YL L )Y# ) yU

    ) (Secure) \)L ;ZL#5 ))YxUU)))# U

    w (Architecture Neutral) #U#U (byte code) x\Zw#;\;L)L U

    \UL (Portable) UZLxYVL)# ))YxU)#J

    . VUYU 15

  • #; (Interpreted) U#;##UUy\ ))YxZU)yUL5 L;)#\ ;\U#;#

    V (High Performance) #;#;\ZU;\#UZU L;U)U;#U )Y)Uy\); ) (Just In Time) ;\ZU5;L)# \U;\;V

    ) (Multithreaded) ;;\ UU) ) (Dynamic) \;\U);\\; (library) L UL

    x\L; C++

    1.8 .

    # (Platform) ##U## (Software Environment) ;\U)\

    ####);\UL #) Windows XP \# Pentium IV L#Z);L;\U))\ )Y;Y###L)Y)V;\ 1.7 x\#;U

    Java Virtual Machine (JVM)

    Java Application Programming Interface (Java API)

    1.7

    16 VUYU .

  • ;\ JVM yL#\UL)#L;\y## L Java API yJ5;\#L ;\y#LU;\)L ;\;\U)LLm)VUU (Graphical User Interface) yU

    ))5#UZ#;U;V Java Platform, Standard Edition (Java SE)

    Java Platform, Enterprise Edition (Java EE)

    Java Platform, Micro Edition (Java ME)

    L#; JVM API ;\L) JLU;\U) Z)#J#;\L) )V;\ 1.8

    1.8 Java 2 %

    ;\U\#)\ U#;\y Java SE L# Java EE JLU);\U\LL (Server) Z)# (Enterprise) # Java ME U)J#5 L )#\;\ PDA yU

    . VUYU 17

  • 1.8.1 Java Platform, Standard Edition# Java SE y#;\U)\U)

    \#)\ Virtual Machine ;\ZU#;Y JVM ; Java API ;\ZUL L java.lang, java.util, java.io java.net

    # Java SE U);\U\#VL Ly 2

    J# (Java Application) ;\U)\ 5 (Java Applet) ;\)5##

    #)LJ Java SE #) 7 (; .. 2010) x\VUU download U;5#;\\ java.sun.com

    1.8.2 Java Platform, Enterprise Edition#JL;Y ;J#\U)Z)\LL JLU

    );\y N-tier Z)# U Application Server Web Server ;\;L Servlet Application Server y;\L);\;\ U Z)ULUL )yL5# (Object Component) ;\;L Enterprise Java Bean (EJB) Web Server Application Server ;\U)wJ);)Y;\ym#U L Tomcat, GlassFish JBoss \U L WebLogic WebSphere Z) #)LJ Java EE Java EE6 (; .. 2010)

    %!

    ) Web Server %19 9 ) Web Server %) Web Server %%%1)() Windows Server, Linux Solaris %) Web Server %>) Java, PHP ASP.NET

    18 VUYU .

  • () Web Server %%) Tomcat ; %) Apache Software Foundation () Linux Windows )>) Tomcat 6.0 (

    % .. 2010)

    1.8.3 Java Platform, Micro Edition# Java ME y#;\U) \U)J#

    \\L L )# PDA (Personal Digital Assistant) yU # Java ME U Virtual Machine ;\;5L JVM ;\U\#)\ )Y;Y \J#L)Y;LZUL ;L;\;5UL

    L\wJ);)#JLLU);\L)L L ;U Symbian L Apple U iPhone L Google U Andriod xZU#))#;xY L# Java ME ;\Z)#LL)Y) Symbian U );# Android ;\)UZ)\;\VL Java SE # Java FX ;\)LJ))5#

    %!

    Android ())%) Open Handset Alliance %) Google, HTC, Intel Samsung (%) Linux )()) )% Android T1-Mobile G1 ) HTC 6.. 2008 %((

    );\)UUY))5# )5U)JU;ym ;\)UJ#L (Complete. Open. Integrated) ZU; API JVM ;\L)V;\ 1.9

    . VUYU 19

  • 1.9 %( Java Technology

    1.9

    ;\VL# Java SE )UV

    1. J# (Java Application) J#U)\ ;YZU#;# x\);Yy stand-alone

    2. 5 (Java Applet) ;\ZU5 ##;\; JVM VL

    1.9.1 %.UJ#;)Y)V;\ 1.10 x\)U);Y1. )Yy;#UU;# ;\ 1.1 y)L

    \#U Hello World ;YU\L HelloWorld x\VU;U;#UU5#;\;\;)\ ; extension y .java L;Y5U\ HelloWorld.java ;Y; main()yJ\U

    20 VUYU .

  • 2. )Y;\y# \#UUVLV#U J) JDK ;\ javac.exe ;\ZU;\y## x\)L;YZ#UZ)\L;Y

    javac HelloWorld.java

    )#;\U#;Y #U;\\ HelloWorld.class ;;\#U;U #JU;\xY \UVU;ZU#UL;\Z#L

    3. )Y) y;U#;#\#U J) Java 2 SDK ;\ java.exe ;\ZU;\y#-;# x\)L;Y)#UUUZ)\L;Y

    java HelloWorld

    U)#)V;\ 1.11

    ( 1.1 Hello World

    1.10 *(

    . VUYU 21

    public class HelloWorld {public static void main(String args[]){ System.out.println("Hello World");}

    }

  • 1.11 * 1.1

    1.9.2 -U5 ;)YU)UJ# L

    5U#;\;xY#

    #U (.java) 5 (.html);\ 1.2 y)L5\U Hello World ;Y

    LJ#;\L; main() LZZ)U LU;UL HTML )LL ;\ 1.3 y HTML ;\;U HelloWorldApplet.class \U5##;\; JVM VL )YU;Y;);Y

    ( 1.2 9 Hello World

    22 VUYU .

    import java.awt.*;import javax.swing.*;public class HelloWorldApplet extends JApplet {

    public void paint(Graphics g) {g.drawString("Hello World",20,20);

    }}

  • ( 1.3 HTML %% HelloWorldApplet.class

    1.12 *-

    1. ;#U (HelloWorldApplet.java) 5 (HelloWord.html) U;#

    2. # HelloWorldApplet.java U#);Y

    javac HelloWorldApplet.java

    )L;YU#U;\\ HelloWorldApplet.class3. U5## L Internet Explorer Netscape m5;\\

    HelloWorld.html x\;#U;\\ HelloWorldApplet.class ));))V;\ 1.13

    . VUYU 23

    HelloWorld Example

  • 1.13 * 1.3 -

    1.10 0 Java API

    Java API yUZ;\LU#L ;\ZU5 5y;\#L ;\;U;\ZU)U;\;) Java API ;- 5;\Z)VL5 L java.lang, java.util, java.awt java.io yU

    \ Java API ;5L VLyZ xy\;\ZL )Y;\;VLU ))YJ) JDK ;#VL Java API ;\VLV# HTML )V;\ 1.14 x\VL Java API U L;\y\5 (JU) L;\y\#L 5 (JLU) L;\y;# () x\V;\ 1.14 VL Java API ;\J#L 5 java.lang ); String

    VL Java API ;L #);Y Z) Z;\)J#)\ \J)L

    24 VUYU .

  • \L \ Constructor L Z;J)L) Z;L) Z; Constructor L)

    1.14 %0 Java API

    %

    $ & ( ) )$ +

    . VUYU 25

  • $ ( $+(&.(

    1(&$

    ( (JVM) $& 1 .( JVM ) Java JVM Java API 4& Java ( 3 Java

    SE, Java EE Java ME JDK (&1

    (javac.exe) ( (java.exe) (&) Java SE ) 4&

    1 JVM +4&1+(&( JVM ) Java API (+ (&()

    JDK

    26 VUYU .

  • 2

    Y;YyZ# x\U))#ZL ;\U UVY UVL;\ ;UZ)\ZL) #)ZL ;UV UVU ZZ)\;\U)UVU x\Y;YyZ;YU\ZLUVL )#

    2.1 ,-.

    y# ;\U))5#;\;LLy# OOP )5#y#wZwLU5#U Ux;UVLV# 5#;Z;\Z)Z 5# (Object) (Class)

    ;#;5# 5#;\VU)Y;Ly instance x\5# Uy instance x\ ;5# U;ZL;\U5# ( instance) U x\x\;\U5#U5#

    UJ) (Attribute) x\5UV;\5VL5# Ly ) (Variable) L;\ (Constant) J);\y);\LU LJ);\yL;\L;\LU (Method) x\5;Z;\VL 5#;\U)Y ;LJ)L)L;;U;\) )L;\\ Student x\;J)\ name ;\\ sayHi ;);Y

    public class Student {public String name;public void sayHi() {

    System.out.println(Hello);}

    }Z)\5LZ)\VZUVL5 {} LJ)

    . Y 27

  • 5VL5 {} Z;)Z)\L;\VL5VL5 {} )\L;J)ULZ)Z

    )\ZU\#\Uy\;) y Case-sensitive LUU))5)LLVU ;;Y;\\ Student ;#;\\ Student.java

    V;UU\;U5# ;UZ)\ new U;Z\ 5#)Y )LLZ)\UL;YyU5# Student ;\\ s1

    Student s1 = new Student();

    ;\); x\y;\V;U\L;\\ String System LZ)J# JVM ;;\;;\\ main x\;V);Y )y

    public static void main(String args[]) {

    }

    ;\ 2.2 )L;\\ Main ;\;;\\ main x\;Z)\U5# Student U;U sayHi )#;Yy#UL Hello )V;\ 2.1

    ( 2.1 )%%% Student

    ;;Y U;5#LLyU

    28 Y .

    public class Main {public static void main(String args[]) { Student s1 = new Student(); s1.sayHi(); }

    }

  • xYx\L Student.java U;;x\L Main.java \U5#U;U;\VL

    2.1 2.2

    )YL#) Java SE 5 UZV;\\ main U;\;U;Vx\);Y

    public static void main(String... args) {

    }

    2.2 .D

    ZU;\; UyZUVVx\L L;Y

    5# (Comment) Identifier ;## (Keyword) ))#Z (Separator) LL (Whitespace) UVL;\ (Literals)

    2.2.1 -. 5#U;\VL x\#LU;YUyLx\

    LU;YL;LZ 5#;U\ \UVULUL\xY LZUU)JyULxY

    . Y 29

  • ZV;5#UV);Y1. 5#Z)U); U\ // ZUU;\U; x\VLL

    )5U2. 5#Z)U)YLx\)xY \UU\ /* YJU\-

    */3. 5#Z)U;\UUy# ;\y# HTML \UU

    \ /** YJU\ */ 5# V;YUy#UU javadoc.exe

    ( 2.2 %91

    2.2.2 Identifieridentifier \;\)YxY x\y\ \) \ \L

    ;\ x\Uy)Y\ );Y identifier UxYUU) A-Z, a-z, _ $ L)Y identifier ;\U))Lx\) )))))YUy))

    UU y) 0 x 9 L)Y identifier UL);##identifier L))#L))#5L) (Case Sensitive) ))Y

    identifier ;\\ myVariable L MyVariable

    )L identifier ;\VU MyVariable _MyVariable $x

    30 Y .

    /* This program is to show how to write comments */public class CommentDemo {

    // Main methodpublic static void main(String args[]) {

    /** This is a comment for documentation */ System.out.println("Document"); }

    }

  • This_is_also_a_variable )L identifier ;\LVU

    My Variable 9pns a+c HelloWorld public

    ,,, );\U)Y\;UZ);Y )Y\ xYUU))#LUU))#5) U))#

    LZ))ZLZ;\\ yZ )LL Main, HelloWorld, Student GraduateStudent yU

    )Y\) \J) 5# xYUU))#5 U))#LZ))ZLZ;\

    \ yZy\)Y )LL x, s1, id, name thesisTitle yU

    )Y\ U);))Y\) LyZ )LL getName, sayHi, setName showDetails yU

    )Y\L;\ U))#L)Y ZU\_ (underscore) yZ )LL MINIMUM, MIN_GPA yU

    2.2.3 .,.;##\;\; #UZ)\;\

    UZZ);##L) UZ;##L U);\ 2.1

    . Y 31

  • 2.1 ;##;\U abstract continue for new switchassert default goto package synchroniedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transientcatch extends int short trychar final interface static voidclass finally long strictfp volatileconst float native super while

    ;##L;YLZ)Yy identifier U x\;##UU5L;##J)y))#5 ;;## goto const y;##;\LU)Z)\ LZL true false LUy;##LyUVL;\ L;)ZL nullx\yUVL;\);\;UVU;\LxL

    2.2.4 .D

    ;))#ZL ;\ZU;U);\ 2.2

    2.2 U;\\L ;\U.D

    ; %,%D0

    ( )D0D 0 if,for H

    , DD

    .%%-. %-

    32 Y .

  • { }

    %-D D0 0-,0 D0 0%D%0 0 if,while for H ,-0

    2.2.5 00 ;\;LL\Z Z)\L U L

    L;L5U )Y;Y#LZL;\yLL;\U)#U LLLZUV#UVLxY x\VLLU

    LL (;# Space ;##) 5 (;# Tab ;##) xY)L (;# Enter ;##)

    2.2.6 0UVL;\Z;\UUV;\y) )) U L x\U

    ZUVL;\U 5 );Y1. (Boolean) 2. )) (Character)3. )Z5 (Integral) 4. ) (Floating Point)5. U (String)

    x\V;UVL;\UV Lx)UL

    2.3 ,

    y;\UJUVL) (Strongly Typed Language) LUV;\y)L;\J);\VL U; UJUVU UVLyL

    . Y 33

  • 1. UVY (Primitive Data Type)2. UVU (Reference Data Type)

    UVYUV;\ZU x\ZU 8 boolean, char, byte, short, int, long, float double );\ 2.3 x\)Y 8 LUy 4

    1. UV (Logical) boolean2. UV) (Textual) char3. UV)Z5 (Integral) byte,short,int long4. UV) (Floating Point) float double

    2.3 LLUVY,

    (,)00 %

    boolean 1 true falsechar 16 \u0000 K \uFFFF

    Unicode byte 8 -128 K +127short 16 -32,768 K +32,767int 32 -231 K +231-1long 64 -263 K +263-1

    float 32 -3.40E+38 K +3.40E+38

    IEEE 754 single precision floating point.

    double 64 -1.80E+308 K +1.80E+308

    IEEE 754 double precision floating point.

    UVUUV\ ;\LLUVY UV)Y;L)\5UVLZ ;;U x\LxL

    34 Y .

  • 2.3.1 ,UV boolean UV UVyUV;\UL

    L5 x\) true false L;\);\;UVy boolean ;LyLx\L;YL)Y ZL true false ULL;## L\yZ;\U (Reserved Word) xLJU)Y\ identifier )Z)Y y;\UJUVL) ))YxUUV boolean ;;\ULUVL)YLJUUUV\ x\;;YL; C++ ;\JUUV)Z5UyUVL;\U

    )LL Z)\boolean flag = true;

    y) flag U;UVy boolean ZU;Ly true

    2.3.2 ,UV char UV)) UV))U\))x\

    ) x\V5VLV Unicode x\; 16 UVL;\x\y)) Unicode VL\ xYUU))# \u U (Hexadecimal number) ;L)YL \u0000 x \uFFFF \5))L U)Y 65,536 ))LL )) Unicode Z);L)YLL \u0E00 x \u0E7F yU

    )LL Z)\char letter = \u0041;

    y) letter U;UVy char ;Ly \u0041 x\;LL))) A

    Z)) ASCII ;\U#)\ )) Unicode ZLL \u0000 x \u00FF UU)) ASCII 128 ) ;Y;\ZUVL;\))Z))\ L x, 1 $

    )LL Z)\char letter = A;

    y) letter U;UVy char ;Ly )) A L;)Z)\LU;Y

    . Y 35

  • ;\;UVL;\;\y)L U U))# \ ZU)))L L \n )Z)xY)L yU ));\U)\y;\ 2.4

    2.4 ));\U)\ Unicode

    \b \u000B Backspace\t \u0009 Tab\n \u000A New line\r \u000D Return\\ \u005C Backslash\ \u0027 Single quote\ \u0022 Double quote

    2.3.3 ,D- UV byte, short, int long UV)Z5UV;Y

    UV;\yZ5 # ;\L;5UVLL)ZU;LUVL) );\LU;\ 2.3 x\)\ZUZ5;UVy int ;UVL;\;\yUV)Z5 L;U\ , (comma) L 10,000 U;y 10000 ;L;\UVU);Y

    1. ;Z5)\ L -121 75362 yU2. ;Z5;\xYUU 0 UU))YL 0 x 7 L

    016 (;LL) 14 )3. ;Z5;\xYUU 0x 0X UU))YL 0 x 9

    )) A x F L 0xA2 (;LL) 162 )

    ) U;UVy)Z5x\ ;UZY;\Z)LZ L5UVUU)UV)Y))L Z)\

    int x = 4;byte b = 4;

    y) x U;UVy int ;Y;\LZ 32 ) b U;UVy byte ;Y;\LZ 8 xUL))Y5L\Uy 4

    36 Y .

  • ) L) x 5UV)Z5L;\UL (-231 x 231-1)

    UVL;\)Z5)\VZU;UVy int LZUVL;\)Z5U;UVy long UL)) l L LU L

    2l x;\;LL) 2 ;UVy long 077L x;\;LL) 63 ;UVy long 0xBAACL x;\;UVy long

    2.3.4 ,,UV)Z (real number) x\#5UV;Y

    LZ;\5UVyLL;\y;) (mantissa) L;\yZZ) (exponent) L;\y;)5LL)YL -1.0 1.0 L;\yZ)5L;\yZ) 5Z IEEE 754 x\LZy single precision double precision ;\) single precision UY;\LZZ 32 LyL;\y;)Z 24 L;\yZZ)Z 8 )V;\ 2.2 L) double precision UY;\LZZ 64 LyL;\y;)Z 53 L;\yZZ)Z 11

    2.2 mantissa exponent single precision

    ZUV)U float double ;\UV float 5UV 32 single precision LUV double 5UV 64 double precision ZUV)Uy;\;\J L 3.14 3.0 ;Y);VLVZ) (exponential form) UU)) E e JZ;\yZ)L 6.02E23 2e-7

    . Y 37

  • UVL;\) )\VZU;UVy double LZUVL;\U;UVy float U L)) f F LU L 2.718F 3.14f yU

    ;YZUVL;\);\;) D d LULyUVL;\;\;UVy double L 3.14D L\)\UVL;\)VZUy double VLU xL;ZyUL)) D d LU

    )LUVL;\);\VU12. 12E2 12.0e2 12.0e2F -3.14F

    )LUVL;\);\LVU1,234.0 1.2E108F

    2.4 0

    UV;\5VLLUV;\yJ)5# J) UV Ly)L;\ x\)UV;\;\LUUZ)\ZL LL;\UV;\ZLU;)Y;L;\LU )Y)L;\U;\UV \;\;Y;\LZZ)5UV

    Z)\);V);Y [modifier] dataType variableName[,variableName];

    ;\ modifier ;##JJ)L )L access modifier L;;\LJL

    y default dataType UV variableName \);\y)Y\)LL Z)\

    int amount;

    y) amount U;UVy intdouble x;

    38 Y .

  • y) x U;UVy double

    ;Y)J U\);\yUV;)U ) Z)\;) )LL

    float price, wholeSalePrice;

    yZ)\) price wholeSalePrice U;UVy float

    2.4.1 DD0 );\;UVU;\Z;\LUUZ)\ZL

    (assignment statement) x\;V);YvariableName = expression;

    ;\ variableName \) expression #x\y)#;\UZU;\UL;\UV

    ) )Z (operator) L x\#y#;\U)#yUVL;\)Z5 ) ) U )Y;YxYVL)UVZ)\;\U

    )LL;YUZ)\ZLZ)) ;\UZUVUUx = 1;radius = 3.14;c = a;y = x+4*3;

    Z)\ZL)\U\#Z)Y Z)## U5)#;\UU)L Z)\

    amount = 121+14;

    ;)YZ)#x\ULy 135 U5)#;\U) amount

    Z)\ZLU;\)VLUU UZ)\L1 = x;

    yUZ)\;\LVU L;\ L;\5)#;\U# (L x) U

    . Y 39

  • ;Y;\Z\);\ZL#U Lx = x+1;

    yZ)\;\U#\L) x ;\;VLU; 1 UZ)#;\U5U) x LL U x ;Ly 5 y 6

    ;\ZL\U) Z)\;)U ;VZ)\);Y[modifier] dataType variableName = expression [,variableName = expression];

    Lint amount = 123;float price = 12.0f;double x = 4.0, y = 2.5;

    ;\ 2.3 y)L;\)UZ)\ZL);\;UVL x\;YU)#)V;\ 2.3

    ( 2.3 )()(

    40 Y .

    public class VariableAssignDemo {public void showDemo() {

    int x,y; boolean b1; float z = 3.414f; /* The program will not be

    compiled successfully if a character f is missing */

    double w; x = 5; y = 4; b1 = (x > y); w = x * 3.2; System.out.println("x = " + x + " y = " + y); System.out.println("b1 = " + b1);

    System.out.println("z = " + z + " w = " + w); }}-----------------------------------------------------------------public class Main {

    public static void main(String args[]) { VariableAssignDemo obj = new VariableAssignDemo(); obj.showDemo(); }

    }

  • 2.3 * 2.3

    2.4.2 0L;\ZUL;## final UZ)\\ ;V);Y

    [modifier] final dataType CONSTANT_NAME = expression;

    ;\ modifier ;##JJ)L L;\L access modifier dataType UVL;\ CONSTANT_NAME \L;\x\)\U))#L ZU\ _ expression #;\U)#yUV;\U)UVL;\)LL Z)\

    final int MINIMUM = 4;final double MIN_GPA = 2.00;

    yL;\ MINIMUM U;UVy int ;Ly 4 L;\ MIN_GPA U;UVy double ;Ly 2.00

    L;\ZLU;)Y; U;ZLL;\;;U x\\ZLUL;\LU

    ;\ 2.4 y)L;\L;\UL;\ x\U)#)V;\ 2.4

    . Y 41

  • ( 2.4 )%

    2.4 * 2.4

    2.4.3 0)L;\x\xYU5Z)\ { } ;\L)Y

    L)L;\y1. )L;\;\yJ)5#J)2. )L;\;\VL5;\;LL;\ (Local Constant) )

    (Local Variable)

    42 Y .

    public class ConstantDemo {public void showDemo() {

    final int MAXIMUM = 10;final double MIN_GPA;System.out.println("Maximum is " + MAXIMUM);MIN_GPA = 2.00;System.out.println("Minimum GPA is " + MIN_GPA);

    // MIN_GPA = 3.00; //illegal}

    }--------------------------------------------------------public class Main {

    public static void main(String args[]) { ConstantDemo obj = new ConstantDemo(); obj.showDemo(); }

    }

  • )L;\ ;\yJ)5#J) );\;\5 x\VL )L;\;Y;UVL ;\J ;UU Z));YVZL\UU))) ;\ 2.5

    2.5 L\U;\VZU)), 0,boolean falsebyte 0short 0int 0long 0Lfloat 0.0fdouble 0.0char \u0000 null

    Z)L;\);\VL5 ;UVL5L)Y ;\ 2.5 )L);\yJ)5#);\y) ;Y;) i ;\yJ)5# L) j k method1()y) ) j method2() 5y);)x\ L;\;U) k method1()U ;Y) j )YLy)))

    ( 2.5 ))

    . Y 43

    public class VariableScopeDemo {public int i; // object variablepublic void method1() {

    int j = 4; // local variableint k = 2; // another local variable

    }public void method2() {

    int j = 0; // local variableSystem.out.println(i); // calling an object variable i

    // System.out.println(k); // illegal}

    }

  • );\yJ)5#5 ;\L;\5ULZU ;\5#)VU )y);\Z5 x\;UVL5;\ZxYL)Y );YVUxY\;;U;\)VL VY\YJZ ))Y)Y;);YL ))\ (temporary variable) );\VL argument 5)VL);YL)

    ;\ 2.6 )LU))5# ScopeExample ;)5# i ;\VUxY\;U5#5VL Heap Memory )V;\ 2.5 \;;U method1() method2() ) i j VUxYVY\)V;\ 2.5

    ( 2.6 )))9

    44 Y .

    public class ScopeExample {private int i=1;public void method1() {

    int i=4, j=5;this.i = i+j;method2(7);

    }public void method2(int i) {

    int j=8;this.i = i+j;

    }}----------------------------------------------------------public class TestScoping {

    public static void main(String args[]) {ScopeExample scope = new ScopeExample();scope.method1();

    }}

  • 2.5 -%%

    ZLU;ZL\UU));\U L;\;UU ;)5#) ZL\UU)));\ 2.5 L;) U;Z)\ZL\U )YU)Y# (Compile Error) )LL Z)\

    public void demoMethod(){int x = (int)(Math.random()*10);int y,z;if (x > 5) { y = 6;}z = x+y;

    }

    ZUL#LU \L y ;;\LVZL\ULU

    2.5 D, #UUVL;\ ) L;\L ;)ZL

    U\Z)#;\yUVL )ZLy 4 1. )Z# (Arithmetic Operator)2. )Z))# (Relational Operator)3. )Z# (Logical Operator)4. )Z (Bitwise Operator)

    . Y 45

  • )Y;Y)Z#)Z U)#yUVZ5Z L)Z))#)Z#U)#yUV

    2.5.1 D,,.)Z#Z)U\ +, -, *, / % )

    ;\ 2.6 2.6 )Z#

    0,.+ a+b- a-b* a*b/ a/b% a%b

    )Z / U)#yZ U)VZ (operand) ))x\yUVZ L;;\)Z)Y)yUVZ5 )ZU)#yLZ5 L 1/2.0 U)#y 0.5 L 1/2 U)#y 0

    )Z % U))VZ;\y)Z5 U)#y)Z5;\y L 7%3 U)#y 1 )ZLLU;)VZ) U)Z + - ;\;)VZ);UL 3 +4.0 yU

    )LL;YyZL ;)ZVL#Z)\int i = 34+2;double d1 = 34.00.2;long l = 300*30;int j = 1/2;double d2 = 1.0/2.0;byte b1 = 20%3;

    46 Y .

  • D,0UZ)ZL (shortcut operator) \U)Z# ;\

    U;\L)UUZ)\ZL L Z)\ x = x+1; x\yZ)\;\U\L x xY; 1 ;LU)ZLU);Y

    x += 1;U;;Z)\U)ZLL ZUZU5xY ;

    )ZL 5 ) +=, -=, *=, /= %= );\ 2.7

    2.7 )ZL 0

    += x += 3; x = x + 3;-= x -= 3; x = x 3;*= x *= 3; x = x * 3;/= x /= 3; x = x / 2;%= x %= 3; x = x % 3;

    D,,00);)ZL;))Z\L (increment operator) ;\U

    \ ++ )ZL (decrement operator) ;\U\ -- )Z)Y)U\L; 1 L; 1

    )Z)YLUUUU))5U )LLx++ x = x+1 ++x x = x+1 x-- x = x-1 --x x = x-1

    )Z\L)ZLU)) )Y;\;UVy)Z5)Z \UUU);UZ\LLUxZZ)\#)Y L\UU)) ;UZ\L )ZZ)\#)Y

    . Y 47

  • ;\ 2.7 )LU)Z\L ;;)#;\UZL\ ++ UUUU)) x\)#;\Uy)V;\ 2.6

    ( 2.7 ))(

    2.6 * 2.7

    2.5.2 D,.)Z))#U;;LUV L U)#

    ;;yUVL;\ Z)Z))#U 6 ) =, == != );\ 2.8

    48 Y .

    public class IncrementDemo {public void showDemo() {

    int x;int y;x = 5;y = x++;System.out.println("x = "+x+" y = "+y);y = ++x;System.out.println("x = "+x+" y = "+y);

    }}------------------------------------------------------public class Main {

    public static void main(String args[]) { IncrementDemo obj = new IncrementDemo(); obj.showDemo(); }

    }

  • 2.8 )Z))# 0 .

    < 0 3= 00 3>=4 false== 0 3==4 false!= 00 3!=4 true

    UV;\Z;;UyUV;\U)L ;;))) ))))) 5#)5# yU )LL

    x > y342

  • ( 2.8 )())

    2.7 * 2.8

    2.5.3 D,.)Z# U))VZ;\y## UV boolean

    )Z;YU)#yUVL;\ Z)Z#U));\ 2.9

    )ZJ)U;)VZ) U)Z;\y)Z;\U)L x\U)VZ;x\);\ 2.10 x ;\ 2.13 )#;\yUVL;\x\UZ;\y)L, AND, OR Exclusive-OR L)L#;\U)Z#;);Y

    50 Y .

    public class BooleanDemo { public void showDemo() {

    int x = 5; int y = 4; boolean b1;

    b1 = (x != y); System.out.println("x not equal y is " + b1);

    System.out.println("y less than 0 is " + (y < 0)); }}----------------------------------------------------------------public class Main {

    public static void main(String args[]) { BooleanDemo obj = new BooleanDemo(); obj.showDemo(); }

    }

  • (7>6) & (26) | (26) U)#;Ly false

    2.9 )Z#

    ! 0&& & AND 0

    || | OR 0^ Exclusive-OR 0

    2.10 )#)Lop !optrue falsefalse true

    2.11 )# AND Lop1 op2 op1 & op2true true truetrue false falsefalse true falsefalse false false

    2.12 )# OR Lop1 op2 op1 | op2true true truetrue false truefalse true truefalse false false

    2.13 )# Exclusive-OR Lop1 op2 op1 ^ op2true true falsetrue false truefalse true truefalse false false

    . Y 51

  • )Z;\;\ && || ;L )Z# short circuit ;\ && y AND L x\L & ;\ && J;;U#)y5 L;) || ;\y OR L x\L | ;\ || J;;U#)y )LL

    int x = 10;if ( (x > 0) || (x++ < -5) ) {

    System.out.println(x); }

    )# x ;Ly 10 \ || L;Z)\#;\

    LZ)\int x = 10;if ( (x > 0) | (x++ < -5) ) {

    System.out.println(x); }

    )# x ;Ly 11

    2.5.4 D,,)Zy)Z;\U)UV)Z5 \))UV

    \\ ;\L );\ 2.14 ;\ 2.15

    2.14 )Z\))UV\

    ~ Complement& AND | OR^ XOR

    2.15 )Z\\\

    >> signed right shift>>> unsigned right shift

  • Ly 0111 L\ >>, >>> > 32);

    x\) x 0 LL 32

    \ >> y\\ x\UUyL 1 5LL 1 LUyL 0 5LL 0 L\ >>> >) Z5UL 2 Z)Z;\\ L\U ( 1 128/21 = 64-128 >> 4 -128/24 = -8128 ) (R to L) ++, --, +, -, ~, ! (data type)2 >)) (L to R) *, /, %3 >)) (L to R) +, -4 >)) (L to R) , >>>5 >)) (L to R) , =, instanceof6 >)) (L to R) ==, !=7 >)) (L to R) &8 >)) (L to R) ^

    . Y 53

  • 9 >)) (L to R) |10 >)) (L to R) &&11 >)) (L to R) ||12 )>) (R to L) ?:

    13 )>) (R to L) =, +=, -=, *=, /=, %=, =, >>>=, &=, ^=, |=

    )LL Z)\x = 2+3*4-(7+2);

    Z)#Z));Y1. Z)#L 7+2 ZUU

    x = 2+3*4-92. Z)#L 3*4 ZUU

    x = 2+12-93. Z)#L 2+12 ZUU

    x = 14-94. Z)#L 14-5 ZUU

    x = 5

    2.6 ,

    ##;)VZ# ;\;UVyL UZU)##y);Y

    ;;\)VZ)Y);UVy double ) ZUU)#;\;UVy double

    ;;\)VZ)Y);UVy float ) ZUU)#;\;UVy float

    ;;\)VZ)Y;UV;\L) ;)UV (type conversion) );Yo U)VZ)x\;UVy double )VZ;)x\VU;

    UVy double ))o U)VZ)YLU;UVy double L;)VZ)x\;\;UV

    54 Y .

  • y float )VZ;)x\VU;UVy float ))o U)VZ)YLU;UVy double float L;)VZ)x\

    ;\;UVy long )VZ;)x\VU;UVy long ))

    o ;\ )VZ)YU;UVy int

    )UU5UL)#;\UZ## ;UVy int yLU ))YZ)\L;Y

    byte b1, b2, b3;b1 = 2;b2 = 4;b3 = b1+b2; // illegal

    xyZ)\;\LVU \ b1+b2 UL)#;\;UVy int x\LZLU));\;UVy byte U

    2.6.1 DD0ZUZ)\ZLU;UV)UUUV#

    UU) Lint i = 4;double x = 3.0;

    ;;\)#;UV;\L) #ZUV)YUU) UV;V

    1. UV;\UxY (widening conversion) UV;\;5LyUV;\;LL

    2. UV;\ (narrowing conversion) UV;\;LLyUV;\;5 x\;U;;UVL

    ZUVL ;\UVUUxYU )V;\ 2.8 x\;));Y

    UV)Z5UyUV)U UV float UyUV double U UV)Z5;;)U);Y

    . Y 55

  • byte short int long

    UV char UyUV int U UV boolean L;))#)UVY\

    2.8 *

    Z)\ZL U)##yUV;\;5LUV) ZUVUyUV))L))L Z)\

    int i = 4;long l = i;

    # i V)UV int Uy long ))

    Z)\double x = 3;

    #;\;Ly 3 V)UV int Uy double ))

    ;;\Z)\ZL;UV);\;5L UV# LUV#Uy;\5L)UV))) L#UU)Y# (compile error) )LL Z)\

    int amount = 123L;

    float f = 4.0;L#LU \UV#;LLUV)

    ;\ 2.9 )LUUV x\ZU;YL# LU

    56 Y .

  • ( 2.9 %%1

    2.6.2 TypecastingZUV UyUV;\;5U U;;\;L

    typecasting x\;V);Y(targetType) expression

    ;\ targetType UV;\U

    U typecasting LZU;\;Z)\x\ZyUUVU;5#LU LZUUVLV;

    )LL Z)\int amount = (int)3.0;

    Z# 3.0 ;\;UVy double Uy 3 ;\;UVy int

    )LZ)\int x;double y = 1.25;x = (int)y;

    Z# y ;\;L 1.25 U;UVy int ;\;L 1 LZU);\yL

    . Y 57

    public class PromotionDemo {public void showDemo() {

    int i;long l;float fl = 4.2f;i = 4;l = i;fl = i;double x = fl;fl = 4.2; //illegal

    }}

  • ;\2.10 )LU typecasting UV # b1+b2 U)#;\;UVy int xUUVUy byte \;\ZLU)) b3 x\;UVy byte U L;))L 3.2 x\;UVy double VUVUy float

    ( 2.10 typecasting 1

    2.7 ,,

    )L;\;\yUV\ x\LLUVY yUV Ux\55# Ly

    1. UV;\y2. UV;\y#)LUV;\y UV String String LLUVY L

    y;\U Java API L Z)\String id;

    yZ)\) id Uy5# String Z))LUV;\y#VLx;\ 8

    58 Y .

    public class TypecastingDemo {public void showDemo() {

    byte b1 = 4;byte b2 = 3;byte b3;b3 = (byte) (b1 + b2);float f1;f1 = (float) 3.2;

    }}----------------------------------------------------------------public class Main {

    public static void main(String args[]) { TypecastingDemo obj = new TypecastingDemo(); obj.showDemo(); }

    }

  • UVU;;5UVLZ;\L 5UVUVY )Y;YUV;\5LZUVUyZLU;\5UVLZ L;UVY UV;\5LZyLUV L Z)\

    int x = 7;String s = new String(Thana);

    y);\;UVY int ;\\ x U5Ly 7 y)UVU (x\55#) s String U5LyUL Thana x\))Y);5UVLZ)V;\ 2.9 x\L;\5) x yLUV;\yL 7 LL) s yZLULZ;\U5UL Thana

    2.9 %-*%

    ) (5#) UVUy;\) ( 5#) LL;Y;\LZ\5UV ;;\))LyJ) 5# J) ZZLU\UU;Ly null )) x\Y;\LZ\5UVL )UVUVUxY \;;UZ)\ new L Z)\

    Date d;

    yZ)\) (5#) d Uy Date ;\ZU Java API ULLZ)V;\ 2.10 ()

    Z)\d = new Date(16, 8, 2002);

    yZ)\Y;\LZ\5J)5#;\\ d ;\;VL 3 ) day, month year x\;UVy int )V;\ 2.10 ()

    . Y 59

  • ()

    () 2.10 %-*%

    2.7.1 String String y;\ZU Java API );\;UVy String 55#x\

    x\ String ;UL5#)\ );Y String y5#;\;L;\UVx\5U ;\VL\ double quote

    ( ) )LL This is a java course

    String y5#;\VUxYZLULZyUUZ)\ new )LL UZ)\

    String s = Thana;

    LZy;\UUZ)\String s = new String(Thana);

    ;;\LUZ)\ new ZZLULZU;\J\ ( ) String Pool L;UVLL ;5UZLU;\YZ) LU)L;5UUxYLZZLUU)Y L;;\UZ)\ new UULY;\LZ ;\ 2.11

    60 Y .

  • )LUZL5# String )Y;;\ULUZ)\ new 5# s1 s2 ;UVLZyZLU;\;) L s3 ;UVLZyZLU;\L)\;UUxYL)V;\ 2.11

    ( 2.11 )9 String

    2.11 %-* String %

    String y5#;\;\LLU (Immutable Object) ZLU)5# String L y;\ZLULZ5#)L LLU;;\LZLU )LL Z)\

    String s1;s1 = Thanachart;s1 = Somchai;

    ;ZUZLULZ5# s1 ;\ )V;\ 2.12 x\y

    . Y 61

    public class StringDemo {public void showDemo() {

    String s1 = "Thana";String s2 = "Thana";String s3 = new String("Thana");

    }}-------------------------------------------------------------public class Main {

    public static void main(String args[]) { StringDemo obj = new StringDemo(); obj.showDemo(); }

    }

  • )L;\ZLU5UV String LZ

    2.12 %%*-*

    String y5#;\;)Z;\U\UUUU) U\ + L

    String s1 = Hello+ World;

    )Z\UU\UV String ))VZ;\yUV\ ;\LL String U x\UV)LUy String ))L Z)\

    String s1 = This;String s2 = s1+ is a test ;String s3 = s1+4;

    ZUUU) s2 y This is a test s3 y This4)Z\\U;)ZL;\U\ += \\U UZL5# String U\L Z)\

    String s1 = This;s1 += is a test;

    yZ)\\U5# s1 )U;\L is a test U55# s1 L ZU s1 ;Uy This is a test

    2.7.2 MathJava API UZU; Math ;\VL5 java.lang x\;L ))

    w#)Z)\#L Math y final J y (;;## static VL x\Lx;\ 6) ;UL;YZULZyUU 5# L;L\UL Math.exp(4.0); yZL exponential

    62 Y .

  • 4.0 Math UL;\));Y

    - final static double E = 2.7182818284590452354;- final static double PI = 3.14158265358979323846;

    x\;U Math.E ;Ly 2.718281828 Math.PI ;Ly 3.141582653

    \ Math ;\Z););Y- static int abs(int x);- static long abs(long x);- static float abs(float x);- static double abs(double x);- static double acos(double x);- static double asin(double x);- static double atan(double x);- static double atan2(double x, double y);- static double ceil(double x);- static double cos(double x);- static double exp(double x);- static double floor(double x);- static double log(double x);- static int max(int x, int y);- static long max(long x, long y);- static float max(float x, float y);- static double max(double x, double y);- static int min(int x, int y);- static long min(long x, long y);- static float min(float x, float y);- static double min(double x, double y);- static double pow(double x, double y);- static double random();- static double rint(double x);- static int round(float x);- static long round(double x);- static double sin(double x);- static double sqrt(double x);- static double tan(double x);

    . Y 63

  • 2.8 D,%.%

    #)\ U;LUVU\U ;Z)#;\U LUVU)#;L J/#J (Input/Output) ;;;))J/#J ;;\;\U)J/#J L;L ;x\UJ (Standard Input) U #J (standard output) ;5#;\yJ/#J5# System.in, System.out System.err

    5# System.in y5#;\;Z)LUVJ#J x\)\5;##

    5# System.out y5#;\;Z)UVJ##J x\)\

    5# System.err y5#;\;Z)U (Error) J#;\UU x\)\Zy

    2.8.1 System.out.println()5# System.out x5#;\\ out x\yJ) System 5#;\

    \ out y5# PrintStream ;\;;\;\U)VL L;\ U)\

    print(String s) println(String s)x\)YUUV;\; argument yUV String

    println() ;UxY)L)#UV;\U print() println() ) argument ;\yUV\ x\yUV String U)))LL Z)\

    int x = 3;System.out.println(x);

    yJZ)\#L) x ;\;UVy int UV int Uy String Ux#U ;\ 2.12 )LUZ)\

    64 Y .

  • System.out.print() System.out.println() \UVL )V;\ 2.13

    ( 2.12 ()

    2.13 * 2.12

    2.8.2 Command Line main() ;\yZL\UZJ# ; argument y String

    args[] x\x)# (Lx;\ 8) args ;\;UVy String x\) argument ;\LL command line U;)LZ)V;\ 2.14 x\yZU args[0];Ly Tommy ) args[1] ;Ly boy ;\ 2.13 y)LJ command line U

    . Y 65

    public class PrintDemo {public void showDemo() {

    int x = 3, y = 4, z = 5;System.out.print("x = "+x);System.out.println(" y = "+y);System.out.println("z = "+z);

    }}---------------------------------------------------------------public class Main {

    public static void main(String args[]) { PrintDemo obj = new PrintDemo(); obj.showDemo(); }

    }

  • ( 2.13 @ command line

    2.14 * 2.12

    %

    y5# ;\;Z;\Z)Z5# ;#;5# 5#;\VU UJ) V;UU\;U5#;UZ)\ new U;U 5# U;\VL x\#LU;YUyLx\

    identifier \;\)YxY x\y\ \) \ \

    L;\ x\Uy)Y\ )Y\

    Z) xYUU))#L Z)) xYUU))#5 U\;\)YxY;L 1 Z xYUZLU))#L UL);##

    ;## \;\; #UZ)\;\UZZ);##L)

    66 Y .

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

    System.out.println("My name is "+ args[0] +" and I am a " + args[1]);

    }}

  • UVL;\ Z;\UUV;\y) )) U L UVLy UVY UVU UVY;\U;)Y 8 char, byte, short, int, long,

    float, double boolean UV;\5VLLy )L;\ x\)UV;\

    ;\LUUZ)\ZL LL;\UV;\ZLU;)Y;L;\LU

    );\;UVU;\Z;\LUUZ)\ZL L;\ZUL;## final UZ)\\ L;\);\VL5 ;UVL5L)Y )Z;\U;)Y 4

    )Z# : +, -, *, /, %, +=, -=, *=, /=, %=, ++ -- )Z# : !, &&, &, || | )Z))# : =, == != )Z : ~, &, |, ^, >>, >>>,

  • 68 Y .

  • 3 %

    Y;YyZZ)\;\;\U)UJ x\yJZ)ZZ)\L LxZ)\UJ Z)\UZx\ULZ)\ if,if..else switch Z)\UZYZx\ULZ)\ while, do..while for U;YLxZ)\UJU

    3.1 D%

    Z)\UJ (Control Structure) yZ)\;\UZZ)ZZ)\L ;UJV

    1. UZ) (Sequential Structure)2. UZ (Selection Structure)3. UZYZ (Repetition Structure)

    )\Z)\;UJZ) ZZ)Z)\;\;VL x\\Z main()ZZ)\ main()Z;Z)Z)\L\ ;;\;;U5# UZZ)\)Y \YJZ)\JU5)ZZ)\;\;U )V;\ 3.1

    3.1 %

    . UJ 69

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

    Student s = new Student();String name;name = s.getName();System.out.println(name);

    }} public class Student {

    String name;public String getName();

    return name;}

    }

  • 3.2 D

    UZyUZJZ)\;;\##;Ly\ x\JZ)\UZUZ)\)L;Y

    Z)\ if Z)\ if..else Z)\ if U (nested if) Z)\ switch

    3.2.1 D ifZ)\ if ;V);Y

    if (logical expression) {statements

    }

    ;\ logical expression ##;\U)#yUVL;\ boolean statements JZ)\

    JZ)\;\VL5 { } Z;;\##ULy x\Z)\ if Z)Zy## (flowchart) U)V;\ 3.2

    3.2 if

    70 UJ .

  • )LUZ)\ if ;);Yif (radius >= 0) {

    area = radius*radius*Math.PI;System.out.println(area);

    }

    )L;YJZ)\;\VL5 (Z)\ZL) area Z)\) Z;;\# radius >=0 y L) radius ;LLL) 0

    ;;\JZ)\5 { } ;;Z)\;;\)\ { } UL Z)\if((x > 0) && (x < 10)) { System.out.println(x);}

    ;LUyif((x > 0) && (x < 10))

    System.out.println(x);

    LL5;;\;U5 { } \UUL LL\Z)\ (LU;Lx\Z)\UU5 { } )

    ( 3.1 )() if

    . UJ 71

    import javax.swing.JOptionPane;public class SampleIf { public void showDemo() { String inputStr = JOptionPane.showInputDialog("Enter value"); int score = Integer.parseInt(inputStr); if (score >= 50) { JOptionPane.showMessageDialog(null, "You pass");

    // null to tell that there is no parent frame } }}------------------------------------------------------------------------public class Main { public static void main(String[] args) { SampleIf obj = new SampleIf(); obj.showDemo(); }}

  • ;\ 3.1 y)L;\UZ)\ if ))U5 x\yUV String ;\5VL);\\ inputStr ;U;\\ showInputDialog JOptionPane UxLLy argument Integer.parseInt() \ZUV String UyUV int UZU) score x\Z)\ if LU) score ;LLL) 50 #U You pass 5;\;U showMessageDialog JOptionPane )L)#;\U);\ 3.1 )V;\ 3.3

    3.3 * 3.1

    JOptionPane

    JOptionPane %( DialogBox %%% %() - showConfirmDialog 9)( yes/no/cancel - showInputDialog 9())1 String - showMessageDialog 9

    3.2.2 D if..elseZ)\ if L;ZZ)\ U##;Ly5 ))YUUZZ)\Z)\\

    ##;Ly5 UUZ)\ if..else x\;VZ)\);Yif (logical expression) {

    true statements} else {

    false statements}

    72 UJ .

  • ;\ true statements JZ)\Z)L false statements JZ)\Z)L5

    ;##ZZ)\;Y)V;\ 3.4

    3.4 if..else

    )LUZ)\ if..else ;);Yif (radius >= 0) {

    area = radius*radius*Math.PI;System.out.println(area);

    } else {System.out.println(Negative radius);

    }

    )L;YU) radius ;LLL) 0 (#;Ly) ZZL ZL#L) area LU) radius ;LUL 0 (#y5) U Negative radius

    . UJ 73

  • ( 3.2 )() if..else

    ;\ 3.2 y)LUZ)\ if..else #U You pass UL) score ;\)U5 ;LLL) 50 #U You fail UL score ;LUL 50 ;)L)#)V;\ 3.5

    3.5 * 3.2

    3.2.3 D if Z)\ if if..else ;\UVLUZ)\ if if..else \UL ##;\

    V;\ 3.6 x\;)YZ ZJZ)\;\ 3 U##;\ 1 y5 LUy)###;\ 2 x\U;Ly ZJZ)\;\ 1 LU;Ly5ZJ Z)\;\ 2

    74 UJ .

    import javax.swing.JOptionPane;public class SampleIfElse { public void showDemo() { String inputStr = JOptionPane.showInputDialog("Enter value"); int score = Integer.parseInt(inputStr); if (score >= 50) { JOptionPane.showMessageDialog(null, "You pass"); } else { JOptionPane.showMessageDialog(null, "You fail"); } }}

  • 3.6 if *

    ##V;\ 3.6 ;\;yZ)\ if UU);Yif (logical expression 1) {

    if (logical expression 2) {statements 1

    } else {statements 2

    }} else {

    statements 3}

    )LLif (radius >= 0) {

    area = radius*radius*Math.PI; if(area >= 500.0) {

    System.out.println(Big Circle); } else { System.out.println(Small Circle); }

    } else { System.out.println(Negative radius);

    }

    . UJ 75

  • 3.7 if *

    UZU);;;\y)##V;\ 3.7 x\;)YZ ZJZ)\;\ 1 U##;\ 1 y LUy5)###;\ 2 x\U;Ly ZJZ)\;\ 2 LU;Ly5ZJZ)\;\ 3 x\;VZ)\);Y

    if (logical expression 1) {statements 1

    } else {if (logical expression 2) { statements 2} else { statements 3}

    }

    ;;Y;\;Z)\ if UUL V if..else if..else ;V Z)\);Y

    76 UJ .

  • if (logical expression 1) {statements 1

    } else if (logical expression 2) {statements 2

    } else {statements 3

    }

    )LUZ)\ if..else if..else ;);Yif (score >= 80) { System.out.println(A);} else if(score >= 70) { System.out.println(B);} else if(score >= 60) { System.out.println(C);} else if(score >= 50) { System.out.println(D);} else { System.out.println(F);}

    )L;Y#)) A U) score ;LLL) 80 #)) B U) score ;L)YL 70 x 79 #)) C U) score ;L)YL 60 x 69 #)) D U) score ;L)YL 50 x 59 #)) F U) score ;L\ZL 50

    3.3 UZ)\ if..else if..else

    ;\ 3.3 )LUZ)\ if..else if..else ;YLL;\U55 #UL Value is one UL;\Uy 1 #UL

    . UJ 77

    import javax.swing.JOptionPane;public class SampleIfElseIf { public void showDemo() { String inputStr = JOptionPane.showInputDialog("Enter value"); int x = Integer.parseInt(inputStr); if (x == 1) { JOptionPane.showMessageDialog(null, "Value is one"); } else if (x == 2) { JOptionPane.showMessageDialog(null, "Value is two"); } else { JOptionPane.showMessageDialog(null, "Other than 1 and 2 "); } }}

  • Value is two UL;\Uy 2 #UL Other than 1 or 2 UL;\UyL\

    3.2.4 D switchZ)\ switch yZ)\UZ ;V);Y

    switch (expression) {case value 1 : statements 1

    break;case value 2 : statements 2

    break; :

    case value N : statements N break;

    default : statements N+1 break;

    }

    ;\ expression #;\U;UVy char, byte, short int L)Y value 1 .. value N UVL;\x\;UV;\U)UV expression

    Z)\ switch L#;\yUV char, byte, short int ;;\L#;L)L;\ 1 ZJZ)\;\ 1 U;L)L;\ 2 5ZJZ)\;\ 2 U;L)L;\ N 5ZJZ)\;\ N L;;\;LL)L Z)\ case 5ZJZ)\ default JZ)\;\ N+1 x\)YZU)##V;\ 3.8

    78 UJ .

  • 3.8 switch

    )LUZ)\ switch ;);Yswitch (x) {

    case 1 : System.out.println(One); break; case 2 : System.out.println(Two); break; case 3 : System.out.println(Three); break;default : System.out.println(Other);

    }

    )L;Y#UL x )YL 1 x 3 #UL other UL x yL\ Z)\ switch ;UZL );Y

    #U;UVy char,byte,short int L)Y UV#L;\ 1 x N UyUV;) Z)\ break JZ5 { } UL;Z)\ break ZZ)\) )L

    . UJ 79

  • Lswitch (x) {

    case 1 : System.out.println(One); case 2 : System.out.println(Two); break; default : System.out.println(Other);

    }

    #U One U Two ;;\ x ;Ly 1 \ default ;L;5U

    ;\ 3.4 )LUZ)\ switch x\U)#yUL Value is one U);\U;Ly 1 yUL Value is two U);\U;Ly 2 yUL Other than 1 or 2 U);\U;L\

    ( 3.4 )() switch

    3.3 DD UZYZ yZ)\;\U)\UJZ)\ ZYZ)Y\;\J x\JZ)\

    UZYZUZ)\ Z)\ while Z)\ do..while Z)\ for

    80 UJ .

    import javax.swing.JOptionPane;public class SampleSwitch { public void showDemo() { String inputStr = JOptionPane.showInputDialog("Enter value"); int x = Integer.parseInt(inputStr); switch (x) { case 1: JOptionPane.showMessageDialog(null, "Value is one"); break; case 2: JOptionPane.showMessageDialog(null, "Value is two"); break; default: JOptionPane.showMessageDialog(null, "Other than 1 and 2"); } }}

  • 3.3.1 D whileZ)\ while ;V);Y

    initial statementswhile (logical expression) {

    statements update statements

    }

    ;\ initial statements Z)\;\UZL\U logical expression ## update statements Z)\;\U;\L

    Z)\ while ZJZ)\Z)\;\L;\VL5 { } ;\##);Ly Z)\;\VL5 { } U;Z)\;\L \;\L##U;Ly5 )YUZZ)\;\VL5L;;\YJ Z)\ while ;y##U )V;\ 3.9

    3.9 while

    . UJ 81

  • )LUZ)\ while \#UL Hello World )Y ;);Yint count = 1;while(count
  • 3.3.2 D do..whileZ)\ do..while yZ)\;\yUZYZ;\;ZU)Z)\ while ;V Z

    )\);Yinitial statementsdo {

    statementsupdate statements

    } while(logical expression);

    ;##ZZ)\;Y)V;\ 3.11 ULZ)\ while )Z)\ do..while Z)\ do..while ZZ)\5 { } LUx\)YUxZ\## ;\Z)\ while Z\##Lx\;LyxZZ)\5 { }

    3.11 do..while

    )LUZ)\ do..while \#UL Hello World )Y;);Yint count = 1;do {

    System.out.println(Hello World); count++;

    } while(count

  • ( 3.6 )() do..while

    ;\ 3.6 )LUZ)\ do..while \#L) 1 x 10 x\U)#L;);\ 3.5

    3.3.3 D forZ)\ for yZ)\;\yUZYZ ;\;VZ)\);Y

    for(initial statements; expression; update statements) {statements

    }

    ;##ZZ)\;YL;)##Z)\ while V;\ 3.9 Z)\ for U;;\Z)YZYZ;\L LZ)\ while do..while U;;\LZ)YZYZLU

    )LLZ)\#U Hello World )Y UZ)\ for LUZ)\ while do..while ;Z)\U);Y

    for (int count = 1; count

  • for (int i = 0, j = 0; i < 4; i++, j += 2) {System.out.println(i + + j);

    }

    yZ)\;\#L) i j ;\ZxYZ)\ for ;\)Y ) i \LxY;x\ ) j \LxY;

    ( 3.7 )() for

    ;\ 3.7 )LUZ)\ for \#) 1 x 10 x\U)#L;);\ 3.5 ;\ 3.6

    );\Z)\ZLZ)\ for ;UU5 { } Z)\ for L)Y U)5 { } compile error ))L;\ 3.8

    ( 3.8 )

    LUZ)\ for ;;\LZ)YZYZ;\L)LL;\ 3.9 yJL));\;LL 1 x 10 UZJL))YLJL)Y;LL) Math.random() y;\UJLZL 0 x 1 Z)\

    . UJ 85

    public class SampleFor { public void showDemo() {

    for (int i=1; i

  • i = (int)(Math.random()*10) + 1; yZ)\;\UJL)L 1 x 10 UZLU)) i ;Y;L

    UZ)\ while U LL;\;UZ)\ for \LZ)Y;\UJL);\LU

    3.9 JL));\;LL 1 x 10

    3.4 (Nested Structure)

    Z)\UJ;U)UL UZ)\ if U Z)\ for U UU;\;Z)\UJ;\VLL)U L Z)\

    for (int i = 1; i

  • ;\ 3.10 y)LUZ)\ for U Z)\ for ;\VL#\ * L)Z)# (5 )Y) Z)\ for ;\VLZZ)\ for ;\VLYZL)Z (3 )Y) U)#)V;\ 3.12

    ( 3.10 )() for

    3.12 * 3.10

    ;\ 3.11 )L;\;Z)\U VVL ;YJLZ5L U#) (Prime Number) ;\VLL)Y)Y x\V;\ 3.13 )L)#;\U);\ 3.11

    . UJ 87

    public class NestedFor { public void showDemo() {

    for (int i=1; i

  • ( 3.11 )()1

    3.13 %* 3.11

    3.4.1 D break, continue label Z)\ break continue yZ)\;\UUZYZ \;\JZZ)\5 { } Z)\ break ZUJYJZUZYZ LZ)\ continue UZZ)\;\5 { } \ZYZLL

    )LL Z)\for (int i = 1; i

  • Z)\ continue );Yfor (int i = 1; i
  • ;;\UJZZ)\UJ;\VL UZ label xY ))L;\ 3.13 x\Z label ;\\ outer ;Z)\ break outer; \;\ZUJZZ)\ for U)#)V;\ 3.15

    ( 3.13 )() break label

    3.15 * 3.13

    %

    Z)\UJ yZ)\;\UZZ)ZZ)\L ;V UZ) UZ UZYZ

    Z)\;\yZ)\UZ Z)\ if, if..else switch Z)\ if..else LZ)\ if ;\ Z)\ if..else ;ZZ)\Z)L5U#

    #y5 LZ)\ if L;ZZ)\U##y5

    90 UJ .

    public class SampleBreak2 {public void showDemo() {

    int i, j, product;outer: for (i=1; i

  • Z)\ if if..else ;\UVLUZ)\ if if..else \U Z)\ switch ;)UZUx)Z)\ if..else if..else .. L

    UV);\ZU)Z)\ switch Uy char, byte, short int L)Y

    Z)\;\yZ)\UZYZ Z)\ while, do..while for Z)\ while LZ)\ do..while ;\ Z)\ while L;ZJZ)\U#

    #y5 LZ)\ do..while ;ZJZ)\x\)YU##y5 Z)\ for ;)Z;\)Z)\ while L;Z)\ZL\U #

    #Z)\;\LU)Z)\ for Z)\ for U;;\Z)YZYZ;\L LZ)\ while do..while

    U;;\LZ)YZYZLU Z)\U)Y;\LUUU ZUL)y)UU L Z)\

    switch VLZ)\ while Z)\ for VLZ)\ for x\;U);YL UU

    Z)\ break ZUJYJZUZYZ LZ)\ continue UZZ)\;\5 { } \ZYZLL

    . UJ 91

  • 92 UJ .

  • 4 ,-.

    Y;YyZ))5# ZL ;\U)5#L 5# J) Z Z)LU J)L5# Z;\UZUZ5# LUyZ)Y)U)5#

    4.1 .,-.

    y# ;\U))5#;\;LLy# OOP )5#y#wZwLU5#U Ux;UVLV# 5# ;Z;\Z)Z 5# (Object) (Class)

    4.1.1 -.

    5#\L ;\;VL;Z)LUy 1. \;\yV (Tangible) \;\y)J)UUL )x ;

    yU2. \;\y (Intangible) \;\L)UUL \ );

    ;\ yU

    5#L UJ) (Attribute) (Behavior) J)5UV5# L;L (Method) \;\5#ZU x\5#5Z)\Z 5#U5#L 5# x\L5#;J)L ;\yUV5# ))UVL;YU;UL

    . )5# 93

  • )L5# )x UJ)L ) \ ;\ ;L

    ; # UJ)L ;\U JL ; ;L \;\ J

    ;Y J) UJ)L \ )J# ; ;L L

    )L5#)L)); x\U5#L L ); (Account) VU (Customer) \;5 (ATM) (Transaction) 5#);;UVL L ;\); \U); );\m); yU 5#);;L L yU

    4.1.2

    )\5#LU5# ;\y;) 5# L;UVJ);\L)L ));; 5# ); 5# ;\L5#;UV;\y;\);\U);;\L) 5#;Z)\U5#U x\y)Z 5#LUUJ)U

    ;#;5# 5#;\VU)Y;Ly instance x\5# Uy instance x\ ;5# U;ZL;\U5# ( instance) U x\x\;\U5#U5# V;\ 4.1 )L Student x\U5#xY 5#;\\ s1, s2 s3 yU

    94 )5# .

  • 4.1 %-

    4.1.3 %

    J)5# UV;\5VL5# x\Ly ) (Variable) L;\ (Constant) ;\J);\y);\LU LJ);\yL;\L;\LU V;\ 4.2 )L5# s1,s2 s3 ;\y5# Student x\;J)5#;\y) \ ;\;\L) ZL) id, name gpa Z)

    4.2 %(-

    . )5# 95

  • 5#UZJ);x\ ;\;L J) (Class Data Value) x\yJ);\J5#UL) L Student ZU;J);\yL;\;\\ MIN_GPA \5L;\)Y\Z)xJ )Y;Y)xJU;L;\VL;\)Y\Z 5J);\)L;YUZUyJ)5# ZUYY;\LZ )U5V;\ 4.3 LUZUyJ) ZU)Y;\LZU)U5V;\ 4.4

    4.3 ( MIN_GPA 45(-

    96 )5# .

  • 4.4 ( MIN_GPA 45(

    4.1.4

    y;Z;\VL5#\U))J) 5#J)L 5#);;J) balance \5;\VL); ; deposit()\y;U)5# ;Z)\\));Z);\LJ) balance ;;U )w#) Procedure Subroutine

    ;5#ZU5#L \) LL (Message) L5#;\yVUL (Sender) )5#;\yVU) (Receiver) ;UL V;\ 4.5 )LLL5# objA ;\yVUL \;Z method4() 5# objB ;\yVU) LLL);LUV objA L) objB L argument ()LL L 1 2 V;\ 4.5) VU)5LL) (Return Value) )VUL

    . )5# 97