Home Map Index Search News Archives Links About LF
[Top Bar]
[Bottom
Bar]
[Photo of the
Author]
Jose M. Fernández

»P§@ªÌÁpô


¤º®e¥Ø¿ý¡G
²¤¶
°_·½
JAVA ¤§¨ú±o¤Î¦w¸Ë¤èªk
µo®iÀô¹Ò
¯S¼x
²Ä¤@­Ó¥Î JAVA ¼gªºµ{¦¡
Á`µ²

Java. Part I

[Ilustration]

¤º®eºK­n¡G ³o¬O¤@¨t¦C¦³Ãö Java µ{¦¡³]­pªº²Ä¤@½g¤å³¹¡C ­º¥ý¡A ¤å¤¤±N¤¶²Ð¨ì¡A ¥Hµ{¦¡»y¨¥ªº¨¤«×¨Ó¬Ý¡A Java ¦³­þ¨Ç¤@¯ë©Êªº¯S¼x¡A ¨Ã´£¨Ñ¬ÛÃö¸ê°Tªº¥X³B©Ò¦b¡C




²¤¶

¥»¤å¬O¤@¨t¦C¤å³¹¤¤ªº²Ä¤@½g¡A ±N¥Î¨Ó¤¶²Ð Java µ{¦¡³]­p»y¨¥¡A »P¨ä¶}µoÀô¹Ò¡C ­º¥ý¡A §Ú·|¹Á¸ÕÁקK°Q½×¦³Ãö¦¹¤@µ{¦¡»y¨¥ªº¾ú¥v¡B ·Lªi¡B ©Î¬O¬~¦ç¾÷ªº¬ÛÃöijÃD ( ¦pªG¦³¤H¯uªº¹ï³o¨ÇªF¦è·P¿³½ì¡A ¨º»ò¥i¥H±q«Ü¦h¥Xª©«~¤¤¡A ±o¨ì³o¨Ç¸ê°T¡A ¬Æ¦Ü¦b¨å«¬ªº¤å¾Ç§@«~¤¤¡A ¤]¥i¥H§ä¨ì )¡C §Ú¤]·|¸ò Duke ¹º²M¬É½u ( Java ªº¦N²»ª« )¡A ¥L¬O¤@­Ó¦³µÛ¥¨¤j»ó¤l©M¤T¨¤§Î¨­Å骺³Ã¥ë¡A ¥¦ªº¤â·|ÀHµÛ°¨ªL¤Úµ^ (marimba) ªº¸`«µ·nÂ\¡A ³oÅý§Ú·Q°_¤F¦Ñ¦¡ªº¬ü°ê¬ì¤Û¤ù¡C

¦b³oªÑ Java ¨g¼é¤¤¡A ¦ü¥G±a°Ê°_¤@¨Ç¬y¦æ¡A ¥«­±¤W¨C¤@¥»¹q¸£Âø»x¡A ¥u­n¯àªg¤WÃä¡A ³£·|³ø¾É¥¦¡A ¤]¦³³\¦h³nÅ餽¥q¡A ª§¬Û¦aµoªí¥i¥Hµo®i Java applets ªº²£«~¡A ¬Æ¦Ü´X¥G¨C­Óºô¯¸³£·|©ñ¤W´X­Ó Java applet¡C

³z¹L³o¤@¨t¦Cªº¤å³¹¡A §Ú±N¾¨¥i¯à¦a¡A ±a»â¤j®a²`¤J¬ã¨s¡A ¨Ã¥B§¹¾ã¾Ç²ß³o­Óµ{¦¡»y¨¥¡C ¹ï°ò¥»ªºªF¦è¨Ó»¡¡A Java ­Ë¬O®e©ö´N°µ¨ì¤F¡A ¦ý¦b¦P®É¶¡¤º¡A ­nºë³q¥¦¬O«Ü½ÆÂø¡A ¦Ó¥B«Ü§xÃøªº¡A ¦]¬° Java ¥Ø«eÁÙ¤£Â_¦bºt¶i¡C

¦b¨t¦C¤å³¹ªº«e´X½g¸Ì¡A §Ú­Ìªº¥Ø¼Ð¡A ¦b©ó´£¨Ñ¹ïµ{¦¡»y¨¥ªº¤@¯ë©Ê¤¶²Ð¡A ±N¨Ó§Ú­Ì¤~¯à¾Ç²ß§ó²`¤Jªº¥DÃD¡A ¹³¬O applets¡B »Pºô¸ô¦³Ãöªº¬ã¨s¡B JDBC¡B Java bean µ¥¡C

§Ú¥²¶·±j½Õªº¬O¡A Java ÄÝ©ó¤@¯ë©Ê¥Î³~ªºµ{¦¡»y¨¥¡A ÁöµM¥¦³Ì±`³Q¥Î¨Ó¼¶¼g applets¡A ¦ý¥¦µ´¤£­­©óºô­¶¼¶¼gªº¥\¯à¤W¡C §Ú­Ì¤£¯à±N Java «]­­©ó¦¹¡A ¦]¬°ÁÙ¦³³\¦h¨ä¥L¥Î³~¡A ¥i¯à³£¤ñ¼¶¼g applets ¨Ó±o§ó¦³½ì¡C

¦b³o­Ó²Ä¤@½g¤å³¹¸Ì¡A §Ú­ÌµÛ­«¡u´y­z©Ê¡v¦h¹L©ó¡u¹ê¥Î©Ê¡v¡C ¦b±µ¤U¨Óªº¤å³¹¤¤¡A §Ú­Ì±N·|²`¤J±´¯Áµ{¦¡»y¨¥ªº°ò¥»¯S¼x¡A ÁÙ¦³µ{¦¡»y¨¥ªºµo®iÀô¹Ò¡C ¥u¦³¦b³Ì«áªº®É­Ô¡A §Ú­Ì¤~·|¬Ý¨ì¤@­Ó java µ{¦¡ªº½d¨Ò¡C

°_·½

Java ¬O¦b¤@¤E¤E¤@¦~®É¡A ©ó Sun ¤½¥q (Sun Microsystem Inc.) ¤¤¡A ¥Ñ James Gosling, Patrick Naughton, Chis Warth, Ed Frank ¥H¤Î Mike Sherindan ³o¨Ç¤H©Òºc·Q¥X¨Ó¡A ªì´Áªá¤F¤Q¤K­Ó¤ëªº®É¶¡¨Óµo®i­ì«¬¡C ±q¤@¤E¤E¤G¦~¨ì¤@¤E¤E¤­¦~¤§¶¡¡A Bill Joy, Arthur Van Hoff, Jonathan Payne, Frank Yellia, Tim Lindolm ¥[¤J¡A ¦X¤O§ïµ½³o­Óªì©l­ì«¬ªº¦¨¼ô«×.

±q³o­Ó®É¶¡¥i¥H²M·¡¦a¬Ý¥X¡A Java ªº¥X²{¤ñ WWW ªº¥X²{§ó¦­¡A ¦Ü¤Ö¥i¥H»¡¬O¦P®É¡A ¦]¬° Tim Berners Lee ¨ì¤@¤E¤E¤@¦~¤§«e¡A ©|¥¼µo®i¥X HTML »y¨¥¡C

Java ªº»yªk¤W¡A ©M C »y¨¥«D±`¬Û¦ü¡A ¨äª«¥ó¾É¦Vªº¯S©Ê¡A ¤]»P C++ ¬Û¥é¡A ¦ý Java ¬O¤@­ÓÅé¨t³s³e§¹¾ã¡A ¦Ó¥BÅÞ¿è©Ê¤@­Pªºµ{¦¡»y¨¥¡C

Java ¥»¨­©M C »P C++ ªº¬Û¦üµ{¦¡¡A Åý¤H«Ü®e©öÁp·Q¬° Internet ª©ªº C++ »y¨¥¡A ¤£¹L¡A ¥¦­Ì¦b²z½×»P¹ê§@¤W¡A ÁÙ¬O¦³³\¦h­«¤jªº®t²§¡C ©M C ++ ¬Û¸û¡A Java ¯S§O§ïµ½»Pºë²¤Fª«¥ó¾É¦Vªº°ò¥»¬[ºc¡C

Internet Åý Java Án¦W¤j¾¸¡A ¨Ï±o¥¦¦¨¬°¤@ºØ¼s¬°¤Hª¾ªºµ{¦¡»y¨¥¡A ­È¦¹¦P®É¡A Java ¤]¹ï Internet ¦³µÛ·¥¬°²`»·ªº¼vÅT¡A ¦]¬°¥¦Åýºô¸ô¥@¬Éªºª«¥ó¡A ¯à°÷¦Û¥Ñ²¾°ÊªºªÅ¶¡½d³ò¡A ¥[¤j¤F³\¦h ( «üªº¬O¡u¯à°÷¦Û§Ú°õ¦æªº°ÊºAµ{¦¡¡v )¡C

JAVA ¤§¨ú±o¤Î¦w¸Ë¤èªk

¦³³\¦h¤u¨ã¡A ¹ï Java µ{¦¡³]­p¨Ó»¡¡A ³£¬O«D±`­«­nªº¡C ªþ±a¦a¡A §Ú©Ò´£¨ìªºª¬ªp¡A ³£¬O«ü¦b GNU/Linux ªºÀô¹Ò¤U ( ©È¦³¤H·|·d²V¤F )¡G

¥Ø«e ELF ª©¥»ªºµ{¦¡¡A »Ý­n¨Ï¥Î¨ì 1.2.13 ª©¥»©Î¤§«áªº®Ö¤ßµ{¦¡¡C

§Ú±N·|¨Ï¥Î JDK (Java Developement Kit) ¤u§@Àô¹Ò¡A 2.0.14 ª©¥»©Î¤§«áªº Netscape¡A ¥H¤Î¤@­ÓÃþ¦ü Xemacs ªº¤å¦r½s¿è¾¹¡C §Ú­Ì¥´ºâ¦b Xwindows Àô¹Ò¤U¶i¦æ¡A ÁöµM¥¼¥²¤@©w³£­n¦p¦¹¡A ¦ýµo®i Applet »PÀ³¥Î¹Ï§Î¤¶­±ªºµ{¦¡®É¡A ´N¥²¶·¨Ï¥Î¨ì¥¦¡C

JDK ¬ÛÃöµ{¦¡¡A ¥i¥Ñ¤U¦Cªººô§}¤U¸ü¨ú±o¡G

http://www.blackdown.org/java-linux.html

±q³o­Óºô§}¡A ±z¥i¥H¿ï¾Ü¥X¤@­Ó¸û¤è«Kªº¬M®g¯¸¥x¡A ¥H§Ö³t¤U¸ü³o¨ÇÀɮסC ´X­Ó¤ë«e¡A ³Ì·sªºª©¥»¬O JDK-1.1.3 ( ³o¤]¬O¥»¤å©Ò¨Ï¥Îªºª©¥» )¡A ¤£¹L¡A §Ú¤W¦¸¤w¦b¯¸¤W¬Ý¨ì 1.19 ª©¥X¨Ó¤F¡C ­È±oª`·Nªº¬O¡A °²¦p§Ú­Ì¤U¸ü¤F©Ò¦³ªºµ{¦¡¡A ¤j·§·|¦³ 24 Mb! ´Nºâ¬O¥u¤U¸ü³Ìºë²ªº³¡¥÷¡A ¤]­n¤j·§ 12 Mb ªº®e¶q ( ©Ò¥H­@¤ßÂI§a! )¡C

¥H gunzip ¸Ñ¶}À£ÁYÀɤ§«á¡A À³¸Ó¤£·|¹J¨ì¤°»ò»Ùê¡A ±µ¤U¨Ó´N¥i¥H¤u§@¤F¡C

³q±`§Ú­Ì·|±Nµ{¦¡³£¦w¸Ë¦b /usr/local ¥Ø¿ý¤U¡A µM«á«Ø¥ß¤@­Ó /JDK1.1.3 ¤l¥Ø¿ý¡A ¦Ó¸Ì­±·|¦³¤U¦Cªº¤º®e¡G

/doc ¥H HTML ®æ¦¡¼g¦¨ªº¥¿¦¡¤å¥ó

/demo Java ¥Ü½dµ{¦¡

/lib ¨ç¦¡®w

/bin ©ñªº¬O JDK ¥D­nªº¤u¨ãµ{¦¡

¦b /lib ¥Ø¿ý¸Ì¡A §Ú­Ì¥i¥H§ä¨ì "classes.zip" ÀÉ®× ( ¤£­n§â³o­ÓÀɸѶ} )¡A ¥¦¥]§t¤F©Ò¦³¤w½sĶ¦nªºÃþ§OÀɮסA JDK ·|¥Î¨ì¥¦­Ì¡C

¦b®Ú¥Ø¿ý¸Ì¡A §Ú­Ì¥i¥H§ä¨ì "src.zip" ÀɮסA ·í¤¤¥]§t¤F classes.zip ¸Ìªº­ì©lÀɮסA ¤£¹L¥¦­Ì¤£¯à³Q®³¨Ó½sĶ¡A ¥¦­Ì¥u°µ¬°´£¨Ñµ{¦¡ªº°Ñ¦Ò¤§¥Î¡C

µo®iÀô¹Ò

¦p¦P«e­±©Ò´£¨ìªº¡A µo®i JAVA µ{¦¡©Ò·|¥Î¨ìªº¤u¨ãµ{¦¡¡A ³£©ñ¦b /bin ¥Ø¿ý¤§¤U¡G

javac : Java ªº½sĶ¾¹¡A ¥i¥H§â Java »y¨¥¼gªº­ì©lÀɽsĶ¦¨ bytecode ªº§Î¦¡¡C

Java : Java ¸ÑĶ¾¹¡A ¥i¥H°õ¦æ bytecode §Î¦¡ªº Java µ{¦¡½X¡C

jre : ¥t¤@ºØ¸ò Java ¬Û¦üªº¸ÑĶ¾¹¡A ¤£¦PÂI¦b©ó¡A jre ¥i¥H¸òµÛ±z¼g¦nªºµ{¦¡¤@°_´²§Gµ¹¨C­Ó¤H¡A ¨º¨Ç¤£»Ý­n©Ò¦³¥\¯àªº¨Ï¥ÎªÌ¡A ´N¯à¤è«K¦a°õ¦æ¡C

appletviewer : ¥Î¨Ó´ú¸Õ»P°õ¦æ applets¡C

jdb : °£¿ù¾¹¡C

javap : Java ¤Ï²ÕĶ¾¹¡A ¥i¥H±N Java ½sĶ¦Ó¦¨ªº bytecode¡A Á٭즨­ì¨Ó¤å¦rÀɧΦ¡ªºµ{¦¡½X¡C

javadoc : ¤å¥ó²£¥Í¾¹¡A ¥i¥H²£¥Í³\¦h HTML §Î¦¡ªº¤å¥ó¡A ¥Î¥H´y­z¤½¶}Ãþ§O (public classes) ©Î¬O¨p¦³Ãþ§O (protected classes)¡B ¬É­± (interfaces)¡B «Øºc¤l (constructors)¡B ¤èªk (methods)¡B Äæ¦ì (fields)¡C ³o­Ó¤u¨ã¤]¥i¥H²£¥ÍÃþ§O¤§¶¡ªº¶¥¼h¬[ºc¡A ¥H¤Î¨ä©Ò¦³¦¨­ûªº¯Á¤Þ¡C

javah : ¥i¥H±N­ì¥Í¤èªk ( ¥Ñ C »y¨¥©Ò¼¶¼g¦¨ ) ¥[¤J Java µ{¦¡¤¤ªº¤u¨ãµ{¦¡¡C

jar : ¥Î¨Ó±N java ¤§Ãþ§O»P­ì©l½Xµ{¦¡¡A À£ÁY¬° Jar Àɮתºµ{¦¡¡CÉ

javakey : ¥Î¨ÓºûÅ@¼Æ¦ìñ³¹ªº¤u¨ãµ{¦¡¡C

updateAWT : À³¥Îµ{¦¡¸Ì¡A ¨Ï¥Î¤F AWT1.02 ªº¤èªk¡A ­n¨Ï¥Î¦¹µ{¦¡¨Ó§ó·s¨º¨Ç¤w§ïÅܪº¦WºÙ¡C

To work without glitches with the current directory structure I advise you to add /usr/local/JDK1.1.3/bin to the environment variable PATH. If you wish to make the development environment available system wide then fix the PATH variable in the /etc/profile file.

¯S¼x

Åý§Ú­Ì¥ý¼È°±¤@¤U¡A ¦V¦Û¤v°Ý­Ó°ÝÃD¡G ¤°»ò¬O Java ? ¨Ì·Ó JDK ¨Ï¥Î»¡©ú©Ò­z¡A §Ú­Ì¥i¥H³o¼Ë¦^µª¡G Java ¥]§t¨âºØ·N¸q¡A ¥¦¤@¤è­±¬O¤@ºØµ{¦¡»y¨¥¡A ¥t¤@¤è­±¤]¬O¤@ºØ¥­¥xÀô¹Ò¡C

¥Hµ{¦¡»y¨¥ªº¨¤«×¨Ó¬Ý¡A Java ¬O¤@ºØ°ª¶¥»y¨¥¡A ¨ã¦³¤U¦C¯S¦â ( ¬Y¨Çµ{«×¤W¡A §Ú­Ì·|¸Ñ»¡¨ä²Ó¸` ) ¡G

  • ²³æ
  • ª«¥ó¾É¦V¦¡
  • ¤À´²¦¡
  • ª½Ä¶¦¡
  • ±j©T
  • ¦w¥þ
  • »P¨t²Î¬[ºc«O«ù¤¤¥ß
  • ¥iÄâ©Ê°ª
  • ¦h°õ¦æºü
  • °ÊºA¦¡
  • °ª¸ÑªR

­º¥ý§l¤Þ§Ú­Ìª`·N¤Oªº¬O¡A Java ¥Hª½Ä¶¤è¦¡¹B§@¡A ÁöµM»¡ Java ­ì©l½X¥²¶·¹w¥ý¸g¹L½sĶ¹Lµ{¡A ¥H²£¥Í bytecode «¬¦¡¤§ª«¥ó¡A ¦ý¦¹ª«¥ó¤w¬O§C¶¥ªºµ{¦¡½X¤F¡A ¥i¥H¦b¦hºØ¥­¥x¤W³Qª½Ä¶°õ¦æ¤F¡C

¥Ñ©ó Java ªº bytecode¡A §Ú­Ì¥i¥H°µ¨ì¡u½s¼g¤@¦¸¡A ¨ì³B°õ¦æ¡vªº¹Ò¬É¡C §Ú­Ì¥i¥H±N Java µ{¦¡©ó¬Y¤@¥­¥x¤W½sĶ¡A ¨Ã¦b¥t¤@­Ó§¹¥þ¤£¦P¬[ºcªº¥­¥x¤W°õ¦æ¡A ¥u­n¸Ó¥­¥x¤W¨ã¦³ Java Virtual Machine (JVM) ¥\¯à¡C Á|¨Ò¨Ó»¡¡A Java µ{¦¡¥i¥H¦b Windows NT ¥­¥x¤W½sĶ¡A µM«á¦b Sun Ultra Solaris ¤W°õ¦æµL»~ ( ¦Ü¤Ö²z½×¤W¬O¦p¦¹ )¡C

¤@¯ë¦Ó¨¥¡A ©Ò¿×¡u¥­¥x¡v«üªº¬O¡A ¬YºØÀ³¥Îµ{¦¡¥i¥H¦b¤W­±°õ¦æªº³nµwÅéÀô¹Ò¡C µM¦Ó¡A ¹ï Java ¦Ó¨¥¡A ¥­¥x¥u¬Oµ{¦¡°õ¦æªº³nÅéÀô¹Ò¡A ¨C­Ó Java ¥­¥x¥²¶·°õ¦æ©ó¬Y­ÓµwÅ饭¥x¤§¤W¡C

Java ¥­¥x¨ã¦³¤U¦C¨â¶µ¤º®e¡G

  • Java Virtual Machine (JVM)
  • Application Programming Interface (API)

JVM ¬O¤@ºØ©â¶H¤Æªº¹q¸£¡A §Ú­Ì¥i¥H¦b¤WÀY°õ¦æ Java ¹w¥ý½sĶ¹Lªºµ{¦¡¡C ¥¦¥»¨­ªº³]­p­ì«h¬O¡u¤p¦Ó²³æ¡v¡A ¦]¬°¨ä¥Øªº¡A ¬O¬°¤F¾¨¥i¯à¦b©Ò¦³¾÷¾¹¤W³£¯à¦w¸Ë¡C ³o¼Ëªººë²­ì«h¡A ¨Ï±o¥¦¾÷·|¯à°÷¦b²{¦³ªº¥­¥x¤W³£¥i¥H¶¶§Q°õ¦æ¡A ¦³§U©ó´£¨Ñµ¹©Ò¦³¤H¤@­Ó¦@³qªº³nÅ餶­±¡A °w¹ï²§½èºô¸ôªºÀô¹Ò (½Ñ¦p Internet)¡A ¥¦¥i¥H¸Ñ¨M¤£¤Ö¥iÄâ©Êªº°ÝÃD¡C ¥Ñ¦¹¥iª¾¡A ³o¶µ¬ì§Þ¤§©Ò¥H¯à°÷¤£Â_±À³¯¥X·s¡A ¨ä¥D­n­ì¦]À³¸Ó¦b¦¹¡C ¶ÈºÞ¡uµêÀÀ¾÷¾¹¡vªº·§©À¡A ¤£¸Ó¥u³Qµø¬°¬O³nÅ餶­±¡A ¦ý¦p¨ä¦WºÙ©Ò¥Ü¡A ¥Ø«e¥¦¤w³v¨BÁÚ¦V§¹¾ã¤§¹q¸£¨t²Î¡A ¨S¹L¦h¤[¡A §Ú­Ì¥i¥H¬Ý¨ì¥«­±¤W·|¦³ Java ´¹¤ù¥X²{¡A ¥Î¨Ó¤ä´©¯u¥¿ªº Java ¾÷¾¹¡B «H¥Î¥d¡B ¹qµø¸Ñ½X¾¹µ¥¡C

Java ¤§ API ¬O¤@­Ó¤j«¬ªº³nÅ餸¥ó¶°¦X¡A ¨ä´£¨Ñ³\¦hºØ¤u¨ã¡A ½Ñ¦p¨Ï¥ÎªÌ¹Ï§Î¤¶­± (GUI) §Y¬°¤@¨Ò¡C ¥¦­Ì¬O¥H¨ç¦¡®w¤è¦¡²Õ¦¨ ( ¨Ì Java ¤§»¡ªk¡A §Y¬° Java ¥]»q )¡A Âǥѳo¨Ç¨ç¦¡®w¡A ¥i¥H¨Ó²£¥Í³\¦hºØ Java ªºÀ³¥Îµ{¦¡¡A ¦Ó¤£­­©ó²³©Ò¬Òª¾ªººô­¶ Applet¡A Á|¨Ò¨Ó»¡¡A ½Ñ¦pºô­¶¦øªA¾÷¡B §Ö¨ú¥N²z¦øªA¾÷¡B ¶l¥ó¦øªA¾÷¡B IRC ¦øªA¾÷¡B ¬Æ¦Ü©Ò¦³§Ú­Ì¥i¥H·Q¹³±o¨ì¡A »P Internet ¦³ÃöªºªF¦è¡C

§Ú­Ì¥i¥H±N¡uAPI ¥]»q¡v¤ÀÃþ¦¨¤U¦C´XºØ¡G

  • °ò¥»Ãþ¡G objects, matrixes, numbers, input and output, data structures, properties of the system, date, time, etc.
  • Applets : set of functions and utilities used to craft Java applets.
  • ºô¸ôÃþ¡G URLs, TCP, UDP, sockets, IP addressing.
  • »P¦h°ê»y¤å¤ä´©¬ÛÃö¡G Helps to write code that can be easily adapted to local specifications and can be visualized in a native language.
  • »P¨t²Î¦w¥þ¬ÛÃö¡G low and high level, including digital signature, public and private keys, access control and certificates.
  • »P³nÅ餸¥ó¬ÛÃö¡G known as JavaBeans.
  • »P¸ê®Æ®w¬ÛÃö¡G provides uniform access to a large number of relational databases.

°£¤F³o¨Ç°ò¥»ªº API ®Ö¤ß¥~¡A ¥t¥~ÁÙ¦³½Ñ¦p 3D ©µ¦ù¥\¯à¡A ¦æ°Ê³q°T (mobile communications)¡A °Êµeµ¥¡C

¦b¥»³¹¸`ªº¶}ÀY¡A §Ú­Ì¤w¸g´£¹L Java ¦³¤@¨t¦Cªº¯S¦â¡A ¥H¤U§Ú­Ì±N§ó¸Ô²Ó¦a¤¶²Ð¥¦­Ì¡G

²³æ¡G

¦b Java ¸Ì­±¡A §Ú­ÌÁ`¥i¥H¥Ñ´X­Ó©w¸q¦nªº®æ¦¡¡A ¨Ó§¹¦¨¬Y¤@¶µ¤u§@¡C ¤@¯ë¥\¯à±j¤jªºµ{¦¡»y¨¥¡A ¨ä©Ò¦³ªº¥\¯à³£¯à¥Ñ Java ¨Ó´£¨Ñ¡A ¦ý Java «o§â¨º¨Ç¤£±`¨Ï¥Î¡A ©Î¬O®e©ö³y¦¨²V²cªº¥\¯à¡A ³£µ¹®³±¼¤F¡C Java Ä~©Ó¤F C/C++ ªº»yªk¡A ¥H¤Î³\¦h C++ ª«¥ó¾É¦Vªº¯S¦â¡A ¥ô¦ó¤@¦ì¼ô±x C/C++ ªºµ{¦¡³]­p®v¡A ³£¯à»´ÃP¦a¾Ç²ß Java¡C ¶ÈºÞ¥¦­Ì¤§¶¡¦³³o¨Ç¬Û¦ü³B¡A Java ÁÙ¬O¥h°£¤F¤@¨Ç C/C++ ªº¯S¦â¡A ¥]¬A¦³¡G

  • «ü¼Ð¹Bºâ
  • Registers (struct)
  • «¬ºA©w¸q (typedef)
  • ¥¨¶° (#define)
  • »Ý­nÄÀ©ñ°O¾ÐÅé (free)
  • ¨S¦³¦h­«Ä~©Ó
  • ¨S¦³¹Bºâ¤¸ªº­«¸ü
  • ¦b unions ¸Ì¨S¦³ structures
ª«¥ó¾É¦V¦¡¡G Java ¥Ñ§¹¥þ¨S¦³ªº±¡ªp¤U¶}©l³]­p¡A ¦]¦¹¹ï©óª«¥ó¾É¦V¡A ¥¦©Ò¨Ï¥Îªºªº¤è¦¡«D±`ªº²M·¡¹ê¥Î¡C Java ªºª«¥ó¼Ò«¬¤Q¤À²³æ¡A ¦Ó¥B®e©ö¨Ï¥Î¡C
¤À´²¦¡¡G Java ¦b³]­p¤W¡A ¥i¥H«Ü®e©ö TCP/IP ¶i¦æ·¾³q¡A ¨Æ¹ê¤W¡A Java ¤¹³\µ{¦¡µo®iªÌ¦b¦s¨úºô¸ô¤Wªº¸ê®Æ®É¡A ´N¦n¹³¦s¨ú¦Û¤v¹q¸£¤WªºÀɮפ@¼Ë²³æ¡C
±j©T¡G Java ¬O¤@ºØ«D±`ª`­«§ÎºAÂà´«ªº»y¨¥¡A ©Ò¥H¦b½sĶ®É´Á (compile-time) ´N·|°µ§ÎºAÂà´«Àˬd¡A ¦b°õ¦æ®É´Á (run-time)¡A Java ¤]·|°µ¤@¨Ç§ÎºA¤WªºÀˬd¡C ¥Ñ©ó Java ª½Ä¶¾¹¹ê§@¤F¦Û°Ê©U§£»`¶°¾¹ (automatic garbage collector)¡A ¦b³o¸Ì©U§£«üªº¬O¤@¨Ç¤£·|¦A³Q¨Ï¥Îªºª«¥ó¡A ©Ò¥H¤£¥²µ{¦¡³]­pªÌ¶O¤ß¡A °O¾ÐÅé¤]·|³Q¦Û°Ê¦aºÞ²z¡C Java ¥»¨­´£¨Ñ¤F³\¦hª«¥ó¾É¦Vªº¨Ò¥~³B²z±`¦¡¡A ©Ò¥H¤@­Ó¼¶¼g¨}¦nªºÀ³¥Îµ{¦¡¡A ¦b°õ¦æ®É´Á©Òµo¥Íªº¿ù»~¡A ³£¥i¥H¥Ñµ{¦¡¦Û¤v¨Ó³B²z.
»P¾÷¾¹¬[ºcµLÃö¡G Java ªº³]­pªÌ³]­p Java ªº¥D­n¥Øªº¡A ´N¬O§Æ±æµ{¦¡¥i¥H°µ¨ì¡A ¡u¥u¼g¤@¦¸¡A ´N¥i¥HÀH®ÉÀH¦a¦b¥ô¦ó¦a¤è°õ¦æ¡v¡C Java ¼g¦¨ªºµ{¦¡½X¡A ·|³Q½sĶ¦¨°ª¶¥ªº byte code¡A ¥¦­Ì»P¾÷¾¹¬[ºcµLÃö¡A ³oºØ byte code ³Q³]­p¥Î¨Ó¦b¥ô¦ó°õ¦æÀô¹Ò ( Java ª½Ä¶¾¹ ) ¤W°õ¦æ¡A µM¦Ó³o¨Ç°õ¦æÀô¹Ò¬O»P¾÷¾¹¬ÛÃöªº¡C
¦w¥þ¡G

·í»Ý­n¤À´²¦¡¸ê°T®É¡A »Ý­n«È¤áºÝ§@·~¨t²Î¦³«Ü°ª¶¥ªº¦w¥þ¾÷¨î¡A ¥Ñ©ó Java ¦b¨ä°õ¦æ®É´ÁÀô¹Òªº¤@¨Ç¯S¦â¡A ¨Ï±o Java ´£¨Ñ¤F¦w¥þ¾÷¨î¡G

  • bytecode ÅçÃÒ¾¹
  • °õ¦æ®É´Á¨t²Î°O¾ÐÅ骺¨ú±o¤è¦¡
  • Àɮצs¨ú­­¨î

§Y¨Ï¸g¹L½sĶ¾¹²£¥Í¤F¥¿½Tªº byte code¡A ¸ÑĶ¾¹¦b°õ¦æ³o¨Ç byte code ®É¡A ·|¦A°µ¤@¦¸Âù­«½T»{¡A ¥H½T«O³o¨Ç byte code ¦Û±q½sĶ¹L«á¡A ¨S¦³³Q§ó§ï¹L ( ¤£ºÞ¬O¤£¬O¬G·Nªº )¡C Java ª½Ä¶¾¹§ó¶i¤@¨B¨M©w¦U­ÓÃþ§O©Ò¯à¨ú±oªº°O¾ÐÅé¡A Java ³Q»{¬°¬O¦b¥ô¦ó¨t²Î¤W³Ì¦w¥þªºÀ³¥Îµ{¦¡¤§¤@¡C

¥iÄâ©Ê¡G ºJ¶} Java °ò¥»ªº¥iÄâ©Ê¨Ó»¡¡A ¬°¤F¹F¨ì¯u¥¿»P¾÷¾¹¬[ºcµLÃö¡A Java ¹ê§@¤F¥iÄâ©Ê¼Ð·Ç¡G ¾ã¼Æ¥Ã»·¬O¾ã¼Æ¡A GUI ¥]¬A¤F©â¶Hªºµøµ¡¨t²Î (AWT)¡A ¦]¦¹¥¦¬O»P¾÷¾¹¬[ºc (UNIX, PC, MAC) µLÃöªº¡C
It is interpreted: To achieve one of the basic goals of Java, platform independence, the java compiler generates an intermediate code (bytecode). It can be executed on any system with the proper interpreter. This paradigm may let us wonder on the possible performance problems of java applications. Nevertheless, and because of this performance concern, Java creators have tried to keep the bytecode design as simple and as easy to translate into machine code as possible in order to keep the highest performance possible.
It is multithreaded: It is simple to write Java applications that can execute several tasks at the same time in a robust way.
It is dynamic: Java does not try to link all the modules that form an application until run-time. This feature helps to link the code dynamically safely and conveniently.

After this long "speech" (much of which can be found in the official Java documentation) one can wonder about the status of Java in the GNU/Linux world. We mentioned earlier the availability of JDK for Linux, which is nothing but a commercial implementation developed by Sun Microsystems (although freely distributable).

There are tools like the compiler GUAVAC under GPL license, that let us compile any Java source without problems. We should also mention a virtual machine called KAFFE under Berkeley license. The last two projects are in a very advanced stage although still need of the Java class libraries from Sun (of free distribution at the moment) in order to have a complete development system.

Several recent projects, still in an early stage, aim at providing development environments to fast and visual applications.

Finally I would like to state that there is a great movement on the net concerning Java and GNU technologies, especially concerning the development of the complete tool box of free-distribution for Java and independent from Sun. We can already understand the importance of Java, and when the fashion is over I am sure that something is going to remain of it (I venture to say a lot of it is here to stay).

²Ä¤@­Ó¥Î JAVA ¼gªºµ{¦¡

After seeing some of the general characteristics of the language let us take a look at a real program. As I mentioned in the introduction, I would like as much as possible to stay away from typical stuff, like the hello world example, instead let us start with a program that draws a square of any dimension we like (a bit complicated but not too much):

File Cuadro.Java
class Cuadro {
public static void main (String args[]) {
        int l ,a;
        if (args.length == 2) {
            l = Integer.valueOf(args[0]).intValue();
            a = Integer.valueOf(args[1]).intValue();
         }
         else {
          l= 20;
          a= 15;
         }
          for (int i=l; i>0; i--){
            System.out.print("*");
            }
            System.out.print("\n");
            for (int i= a-2; i>0; i--){
             System.out.print("*");
               for(int j=l-2; j>0; j--) {
                  System.out.print(" ");
                  }
                  System.out.print("*\n");
                  }
                for (int i=l; i>0; i--){
                System.out.print("*");
                }
              System.out.print("\n");
           }
}

In Java the name given to the source file is very important because it defines a "compilation unit". The file may contain one or more definitions of classes. The compiler expects the source file to have the extension .Java (4 characters) consequently some systems do not support it (DOS or Windows 3.1)

In the example given the name of the class defined coincides with the name of the source file. This is not by chance, in Java all code must be in a class. By convention the name of the class has to coincide with the name of the source file containing the class.. Moreover Java is case sensitive (upper or lower case).

To compile the example from the command line type:

> javac Cuadro.java

The Java compiler (if everything went OK) will generate the file Cuadro.class that naturally contains the binary (bytecode) form and it can be immediately executed by the interpreter as:

> java Cuadro

When java source code is compiled, each class in individually placed in an archive with the same name as the class. It is then a good habit to give the source files the same name as the class contained in them so that the source filename will coincide with the archive.class file.

Despite the simplicity of our example Cuadro.java it helps us understand the fundamentals of Java and in particular the structure of a typical java program.

First take into account that this example is not an applet that can be included in a HTML file but an independent program to be executed by the java interpreter on the command line.

The first line of the program is:

class Cuadro {

The first word is a reserved token of the language, it indicates the definition of a new class named in this case "Cuadro" -- Square in spanish -- The exact definition of the class including all its members follow in between two curly brackets {}. Note that in Java all the activity of a program resides inside a class.

In the following line:

public static void main (String args[]) {

it declares a method named main(). All Java applications start their execution with a method called main() (similar to C/C++). Next let me comment a few details necessary to understand the example (in later articles we will explained them in more depth).

The keyword public control the scope of the member methods of the class. When a class is declared public, then the class can be used by code defined outside the class. The keyword static forces the main() method to be invoked without needing to instigate the class. Void indicates that this method does not return any value. To pass parameters to a function or method one uses the parenthesis written after the name of the function, in our example main takes as parameter a matrix of instances from the class String.

Obviously all the code belonging to the method is also enclosed between curly brackets. Next, in the line:

 int l, a ;

we declare two integer variables. In Java every variable must be declared before it can be used. Notice each instruction in the code ends with a semi-colon ";".

The remains of the code implement a small algorithm that :

  1. Test whether the right number of arguments have been provided
    If (args.length==2)
    
  2. If parameters are provided, then they are assigned to the integer variables:
      l = Integer.valueOf(args[0]).intValue();
      a = Integer.valueOf(args[1]).intValue();
    
  3. Otherwise the variables are initialized to default values:
      l = 20;
      a = 15;
    
  4. The rest of the program is nothing more than the instructions needed to draw a square on the screen. We can point specially to:
    System.out.print()
    

    where print() prints the string passed as argument. System is a predefined class that gives access to the system and out is the output stream connected to the console. Also,

    for (int i=l; i>0; i--)
    

    As expected it behaves as our experience with C/C++ and other languages tells.

A nice piece of homework the reader can do using this little code is to test how to compile it and execute it, but also to test the portability of the code by running the compiled object Cuadro.class under various platforms: Linux, windows 95 (but don't take this as a present!) and see that it runs the same on any platform.

Á`µ²

§Ú­Ì¤w¸g¥Ñµ{¦¡»y¨¥ªº¨¤«×¡A ¤ÀªR¤F¤@¨Ç Java ªº¯S¦â¡A ¦Ü¦¹¡A §Ú­Ì¤~·Ç³Æ¦n¶}©l¥i¥H¼gµ{¦¡¡A ©¹«áÁÙ¦³«Üªøªº¤@¬q¸ô­n¨«¡A ±µ¤U¨Óªº¤å¥ó¤¤¡A ±N´£¨Ñ§ó²`¤Jªº»¡©ú¡C ¹w§i¤U¤@½gªº¤º®e¡A §Ú­Ì±N¬ã¨sÅܼƪº©w¸q¤è¦¡»P¨äÃþ«¬¡A °ò¥»«ü¥O¡A ¥H¤ÎÃþ§Oµ¥¡A ³o¨Ç³£¬O Java ³Ì¥D­nªº¿W¯SÂI¡C

¨C½g¤å¥ó·í¤¤¡A §Ú³£·|ªþ¤W°Ñ¦Ò¸ê®Æ¡B ¥Xª©®ÑÄy¡B ºô¸ô¸ê·½µ¥¡A ³o¨Ç¬O§Ú¼¶¼g¦¹¤@¨t¦C¤å¥óªº°ò¦¡C

  • JAVA Reference Manual. Patrick Naughton Herbert Schildt. McGraw-Hill.
  • Programming with JAVA. Tim Ritchey. Prentice Hall.
  • Tutorial de Java . Spanish manual in HTML format that can be obtained at www.fie.us.es/info/internet/JAVA. Agustín Froufe.
  • The Java Tutorial. English manual in HTML www.javasoft.com. Sun.
  • Tutorial from the JDK distribution.

¥»¤å¥Ñ Penelope Marr ©Ò½Ķ


¥Dºô¯¸¥Ñ Miguel Angel Sepulveda ºûÅ@
© Jose M. Fernandez 1998
LinuxFocus 1998