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����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

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�Dz� 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 �ڭ̤~�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

  • 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