Jose M. Fernández ¤º®e¥Ø¿ý¡G ²¤¶ °_·½ JAVA ¤§¨ú±o¤Î¦w¸Ë¤èªk µo®iÀô¹Ò ¯S¼x ²Ä¤@ӥΠJAVA ¼gªºµ{¦¡ Á`µ² |
Java. Part I¤º®eºKn¡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 ¥un¯àª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´yz©Ê¡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 ±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 ¥Dnªº¤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´yz¤½¶}Ãþ§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
º¥ý§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 ¥un¸Ó¥¥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
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 ¨ä¥Dnì¦]À³¸Ó¦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
°£¤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
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): 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.javaThe 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 CuadroWhen 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 :
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 ³Ì¥Dnªº¿W¯SÂI¡C ¨C½g¤å¥ó·í¤¤¡A §Ú³£·|ªþ¤W°Ñ¦Ò¸ê®Æ¡B ¥Xª©®ÑÄy¡B ºô¸ô¸ê·½µ¥¡A ³o¨Ç¬O§Ú¼¶¼g¦¹¤@¨t¦C¤å¥óªº°ò¦¡C
¥»¤å¥Ñ Penelope Marr ©Ò½Ķ |
¥Dºô¯¸¥Ñ Miguel Angel Sepulveda ºûÅ@ © Jose M. Fernandez 1998 LinuxFocus 1998 |
mirror server hosted at Truenetwork, Russian Federation.