Jose M. Fernández ���e�ؿ��G ²�� �_�� JAVA �����o�Φw�ˤ�k �o�i���� �S�x �Ĥ@�ӥ� JAVA �g���{�� �`�� |
Java. Part I���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����Dz߳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�ӧڭ̤~��Dzߧ�`�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 �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
�����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
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
���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 �ڭ̤~�dzƦ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�dz��O Java �̥D�n���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 |