Authorized translation from the English language edition of · Authorized translation from the...

50

Transcript of Authorized translation from the English language edition of · Authorized translation from the...

Page 1: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking
Page 2: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� ��

Authorized translation from the English language edition of:Thinking in JavaBy Bruce Eckel, President, MindView, Inc.Published by Prentice Hall PTR �Copyright 2000 By Bruce Eckel, President, MindView, Inc.

� Java�������� ��������������

� � ����������������������

� ������������

�����������������������������

���������������������

� � �

�� ���������!�������������������"������#��

�������������������������� ���"��$����

�����������"������� �������������� ���"��� ��

� %�������������������������

��&�� '�"(���'�������"�������������������

� ��&)*�&�����������) �) ����+�,�

�http://www.focus.co.il�� �� �������� [email protected]����� ������

������������ �����������������������

� ���� ������������� �����������������

�����

� ����#������

������ ��������

� �����������

��������������

� �� ����

�������������� ��

"#��������������

� www.silvia.co.il

�������

� ����������%�

�������

� ����� ����S�

������

� �����#�����������

������������ ��

��$�������

�������������

� � ��)*�''**��,� �

Page 3: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� ���������

� � .................................................................................... �� ��

� ............................................................. ��������������� �����

�� ..........................................................�������������� ��� ������

��................................................������������������� ������

��..................................................................������������ � ����

���............................................................. ��������!� ������

���......................................................����������������� �"����

� ���.................................................................... �����#�� �$����

��...............................................�������������������� ������

� .................................................. %������������������ ������

�....................................... ��&��������������&�������� ������

��........................................... ����'&�������(��������� �����

���...................................................������������!���������� ������

���................................................... ��������������������� ������

$��............................................................���������������� � ����

$$�................................................................. ����������� ������

Page 4: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � ��

� �"�..........................................���������������������������'����!�

� ��...................................... )JNI*�����'&������������������'����!�

� ���.............................................����'&������������������ �'&���!�

� ���.......................................................................��������' ���!�

� ��...........................................................................�������������

Page 5: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� �����������

� �� .................................................................................... � ���

�� ............................................................. ������������ ������

�� ................................................................................������������

�� ...........................................................................�� ��������������

�� ....................................................................................��� ��� � �

�� ...............................................................................� � ������ ��

�� ...................................................................�� ������ ���������� .................................... ���� ���������� �������������������

�� .................................................. �������� �� ��������������!"���� ...................................................... ���� ����� ���������

�� .....................................................����������������#������ ����� ...........................................................................���������������$ ...................................................................... ������������������%& ...................................... ����������� ������� �������������

%� .....................................................������������ ���������������%� ...............................................................................� � �����������

%� ..................................................................................................�� ��

%� ...................................................................................� �� ������'�%% ..................................................................................(� �� ������ %� ..............................................................................�����!��� ���� ...............................................................................������!��� ���� ...........................................................................�� �������������

�� .......................................................................................... ) ����� �% ........................................................................... �� ���#����&������ .....................................................................(�� �� ��� ��������$ ......................................................................(����� � ��!����������� ........................................................................)���������� ���������� ......................................................� �� ������������� ��������� ................................................................................��!�����%�����% .................................................................................. ���� �) ���

Page 6: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �

�% ......................................................................................... ������� ���% ................................................................... ����� ����������������� .....................................................................................������ ��

�� .......................................................................... ��'�������!������� .............................................................���� �)������������������ ....................................................................������������� ��� ��� .................................................................................. ������������� ...................................................................................... ������ ��

�$ .................................................................................. ��� �����������$ ..............................................................................................�� ��� .............................................................................. ������ ������

�� ...............................................................................(C++�� �����'�

�� ....................................................................................................����

�� ..........................................................���������������� ������

�% .................................................... �� �����! �������� ����������

�� ....................................................... �����������������!���#�������� .................................................................. )�����!�� ����� ��� ...................................................... ���������� � �������� ���� $& ................................................................................. ��'��������

$& ..........................................................��������������#��!���������$& ....................................................................................... �������$� ...................................................................������������������

$� .......................................................��� ��������� � ����������!�$� .....................................................................................��������

$� ..............................................................������������� ����*����$% ........................................................................... ��� ������ ���

$� ............................................................................... ��'���� ������ �$� ..................................................................................... � ��� � �$� ...................................................................... ���� ���������� ��

$� ........................................................................... static��� �����

$$ ...................................................................#���� �������'���� ���&� ....................................................................................�!�������

�&� ........................................................................... ��� �����������&� .................................................................... �������! ��������&� .............................................................................................�����&� ..................................................................................��� �HTML�&� ........................................................ ������� ���� ���"�see+�&� ............................................................................ ��� ���������

Page 7: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � �

�&% .......................................................................�� �� ������������&% ......................................................................... ����������������&� .................................................................................. ������� ��

�&� ..........................................................................................�����) ��

�&� ..................................................................................................����

�&� ...............................................................................................�������

���................................................����� ������������ ������

�&$ .............................................................. ��'��������������� ����& ............................................................................................� �����& ................................................................................� ����������� ........................................................................���� � ����������% .............................................................�� ���)���������������� ........................................................................... ���������������� .............................................................................���������������& ..........................................................................����������������� ...........................................................................�������������

��% ............................................................... ,if-else-��� ������������� ............................................................................... �������������� ........................................................................���� ���������� .............................................. ���������� ������������� ��� ........................................................................... �� ����������

��& ................................................................... sizeof�)�����'��������& ........................................................................ � ���������� ������ ............................................................................�������������

��� ................................................................................ �!��������������� .......................................................................................����� �

��� ............................................................................................ if-else��� ............................................................................................����

��% ........................................................................................ do-while��% ..................................................................................................for��� ............................................................................ continue"�break�%� ............................................................................................switch�%� ..................................................................................................����

�%� ...............................................................................................�������

���..................................................................������� � �!����

�%$ ............................................................. �� �����! ����� ����

��� .....................................................................................������� ��

Page 8: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �

��� .............................................................. �� � ������)���� ����� .............................................................. ���������� � ������ ����� ......................................................................... ������������ ����� ...........................................................................�� ���������� �

��$ ............................................................................ this��� �����

��� ...................................................................�����/�����������)���

��� ...........................................................(finalize,-���������� �� ���� ..................................................................... )��� ��!���#��������� ...................................................................................��������� ���$ .................................................................... �����/�� ������!��

��� ...................................................................................... ����������� .............................................................................. ���������)�!��% .........................................................................�� �������������

�$� .................................................................................... ����� �����$� .......................................................................... ���� "��������

�$$ ..................................................................................................����

�&& ...............................................................................................�������

���.............................................................� � ���"� ������

�&� .......................................................................... ������������������&� ............................................................. ������������� �����!��&$ ......................................................................... �������������������& ..................................................... ��� ���� ������������� ����� ................................................................................. ����������

��� ........................................................................��'������������ ��! ��� ............................................................................................ ������

��� .........................................................................�� �������public��% ...................................................................... 0��������� ��private��� ..................................................................�������������protected��� ......................................................................................� � ���

��$ ......................................................................................��� �����

��� ..................................................................................................����

��� ...............................................................................................�������

���......................................................�� ���� �� �� ������

��� ..................................................................................... �����������

��$ .....................................................................................������������� ...................................................................... ��������� ����

Page 9: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � �

��� ..................................................................... ��������������������% ...........................................................................)����)��� �������$ .................................................................................. � �������

��& .....................................................................������������)�������

��� ........................................................................................... protected��� ...................................................................................... ���! ����

��� ....................................................................................... ��� ���� ���� ........................................................................ ���� ���� �����

��% ...............................................................................final��� ����� ��% ..................................................................................�������� � ��$ ..................................................................................����������%� ............................................................................... ��������

�%� ................................................................................. ��������final�%� ...................................................................��� ����� ��������%� ............................................................................ �����������

�%% ..................................................................................................����

�%� ...............................................................................................�������

� ���.................................................................... ��#$�� ������

��& .......................................................... ��� ���� �������� �� ����� .............................................................. �������������������

��� .................................................................................................1������ ...................................................................������������)���������� ...................................................................� � ����� ����������� ...................................................................................���������

��$ ...........................................................................�� ���� ��������

��� ....................................................................���� ���������

��� .............................................................................. ����!"������ ���� .......................................................................��� �������������

��� ................................................................... finalize,-������������& ......................................��� ��#�������!"�������������� ��

��� .................................................................................. ��������) ����� ............................................................ ������ ��������������% ...............................................�!���) ����������������� ���� �

��� ..................................................................................................����

��� ...............................................................................................�������

Page 10: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� ��

���...............................................� ������ ����� ������

�$� ..............................................................................................���� �$% ....................................................................��'������� �������$� ........................................................�������! ����� ������$$ .................................................................................. ������1����&� ....................................................................���� ����������&� ..................................................................................���� �) ��

�&% .................................................................................. �� � ����� �&� .........................................................��� ���� ���� � ����� �&$ ...............................................���������#����� � ����� ��� ...............................................................�� � ���� � ����� ��� ................................................................�� !������ ���������� ..................................................................�������� � ����� ��$ ...................................................�� !������ ������������ ����& ....................................� ����� ��� � ����� ��!���� ����� ..................................................................�� � ����� �������� ......................................................... (�� � ����� ������)�� ���� ...................................................................... �� � ����� ���� ��� ................................................... (�� � ����� ��� ������� ��$ ........................................................��������� ��� � �����

��� ..................................................................................................����

��� ...............................................................................................�������

�!�.................................................. %��������������� � ������

��� ............................................................................................... ����� ��� ............................................. � ������� ������������������ ��� .................................................................................... #�� �����

��� ..............................................................................Arrays���� ���& ...................................................................................... #�� ���� ��� ...................................................................................#�� ��������� ......................................................................... ����� �)���������� ................................................................ ����� �������)���������� ........................................................................................ #�� �)� ��� ..........................................................................)� �#�� �������& ............................................................................... �����"������

��& ..................................................................................... ��� ���� ��� ................................................................................ ��� ��������� ................................................................................... ��� ����

��$ ......................................................... ���������������� ����)����� ...................................................................��������������������

Page 11: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � ��

��� ........................................... ����������� �#�� �� ����������!�

��� .......................................................................................... ��������

��� ..................................................................................��� �������

�$& ................................................................/����������"�Collection

�$� ........................................................................� �����������"�List�$� .................................................���� �� ����#� ��� � ����!��$$ .......................................................���� �� ����#� �������!�

�$$ ......................................................................... �!����������"�Set�&� .......................................................................................SortedSet

�&� ......................................................................... ��� ��������"�Map�&� .....................................................................................SortedMap�&� ..............................................................................�������������

��� ............................................................... hashCode,-������������

��$ ..................................................................................... �� ���������� ...............................................................................�������"���

��� ..............................................................��������������� �� ��

��� .......................................................................................� � �������% ............................................................................� ����)���������� ............................................................................ �!���)���������� ............................................................................����� �)�������

��� ........................................................................... � ���������)�

��% ..................................................................................................�������% ........................................ � ������ �� ���������� ���/����������� ..................................................................��� ���/������)�� �

��� .............................................................................. �� � ��������

��� ...........................................................................�2&3�2����'�����

��� ................................................................... Enumeration"�Vector��� ...................................................................................... Hashtable��� ............................................................................................. Stack��� ............................................................................................ BitSet��% ..................................................................................................����

��� ...............................................................................................�������

!��....................................... ��&�� ��# �����&������� �������

�%� ................................................................................... ��������������%� ....................................................................... ����������� ���

�%� ........................................................................................����������

�%� ........................................................................................ try����

Page 12: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� ��

�%� ................................................................. ���������������!� �

�%� ..............................................................................#�� ����������!�

��& ....................................................................................... ��������� ��� ............................................................................������������������ ..................................................................... ����������������

��� ......................................................................��'�������� ��������

��� ................................................RuntimeException������ ����� �

��� .............................................................. finally���! ���)��� ��!��

��& ......................................................................... (finally���� �� ���� ......................................................................�����������������

��� ............................................................................... �������������

��� ................................................................................................. ��� �

��& .................................................................................... ������� ������ ...................................................................�������� ������ �

��� ..................................................................................................����

��� ...............................................................................................�������

!��........................................... ��'&�������(�������� �������

��� ...................................................................................... File���� ���� ...........................................................................����������������$& ....................................................................... ���������!�����

�$� ............................................................................................�������

�$� ................................................................InputStream����������

�$� ............................................................. OutputStream����������

�$% ....................................................���� ������� ��� ���� �����

�$� ........................ FilterInputStream���! ���InputStream" ������

�$� ................... FilterOutputStream���! ���OutputStream"�������

�$$ ..................................................................Writer"�Reader���� ��$$ .................................................................�� � ������������� %&& ...................................................................������������ ���� ��%&� ........................................................................ �����������

%&� .......................................................RandomAccessFile������ �����

%&� ....................................................���3������ �������� �������� ��%&% .........................................................................................����� ��%&� .........................................................................................����� ��%&$ .............................................................................. ����!����� ���

%&$ ..................................................................................... � ������3���%&$ ............................................................................� ������ ������

Page 13: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � ��

%�& ....................................................PrintWriter"��System.out������%�� .........................................................� ������3��������� ����

%�� .................................................................................................����%�� ....................................................................GZIP�������������

%�� ............................................................Zip������!���� ����)��%�� ......................................................................... ,JAR-���'��������

%�� ................................................................................��������������%�� ...............................................................................��� �����! %�� .............................................................................. �������������%�� ............................................................................... �� ����� ��

%�& ....................................................................�� ���������������

%�� ...........................................................................StreamTokenizer%�� ............................................................................ StringTokenizer%�� ...........................................................�������� ����) ��������

%%% ..................................................................................................����

%%� ...............................................................................................�������

���...................................................�#���� �����"�������� �������

%%$ ......................................................................................RTTI"��#�!�%�� .................................................................................Class�������%�� ................................................................... �� ���!���� ��������

%�� ........................................................................................ RTTI�����

%�% ............................................................�!���) ������ ���� ��/���%�� ........................................................................��� ������1��

%�� ..................................................................................................����

%�� ...............................................................................................�������

���................................................... ��� ������ ���#� �������

%�� ...................................................................................... ������) ���%�� .......................................................................�� ����������� %�� ...................................................................... �� �������� ���%�$ .................................................................................���������� %$& ................................................� �� ���)�����#����� �����!��

%$� ...................................................................Appletviewer"��� ��%$� ..............................................................................�� ���������

%$� .......................................................���������#� ��� �����!��%$% .................................................................................. ��!������

%$� ......................................................... Windows Explorer�"��� ��

%$$ ..........................................................................................)!�����!�

Page 14: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �!

�&& ........................................................................................ ����������

�&� .........................................................................................���������

�&% .................................................................................... ������������

�&% ................................................................................BorderLayout�&� ...................................................................................FlawLayout�&� ....................................................................................GridLayout�&� ............................................................................. GridBagLayout�&� ................................................................................. ��� ���!�

�&� .................................................................................... BoxLayout��� ........................................................................... (�!� �������

��� ................................................................... Swing�������������� ��� ............................................................���4 �5 �6 �������������������� ..................................................................������������������

��� ........................................................................ Swing����������������� ............................................................................................�� !�

��� ...................................................................................... ,� �-�Icon��$ .......................................................................................���"�������& .....................................................................................����������� .............................................................................................������� ................................................................................ ���� ��� ���� ...................................................................... ����� � ������#����� .................................................................................... ) ���������$ .............................................................................. �������� !���& .................................................... ,��� �� ���-����� �������� ...................................................................................� ����������� ...............................................................................��������������% ................................................................................... ������������ ..........................................................................................��������%� ............................................................................��!�� ���������%% ............................................................................................ �����%� .................................................................................. ��"��������� ......................................................................��!������"������

��% ...................................................................... Swing��������HTML��� ....................................................................� ����������� ��� ...............................................................................................��!���& ...........................................................................................�������� .......................................................................������� �������% .....................................................................................���������

��� .....................................................................JAR�1����) ���������

��� ...............................................................................� ��������� ��

Page 15: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � ��

��� ...................................................................����������� ���������& .....................� �� ���� ���������� ��������������������

��� .......................................................... ,canonical form-���������!

��� .......................................................... JavaBeans�������������� ��

��% ......................................................................................(Bean��

��� ...........................................Introspector���! ����������� �1��

�$� .........................................................������� �JavaBeans������$� .................................................................... JavaBeans������������$$ ...........................................................��������� ������������ �

�&& ..................................................................JavaBeans�������������&& ..................................................................................................����

�&� ...............................................................................................�������

���............................................................��� ����������� ��!����

�&� ..................................................................����"��� �� �� ���

�&� ................................................................... Thread���� �������& .................................����"��� ��� ����!���� � ��������� ����� .......................................... ���������� ������ � ��#����������% ................................................................... ������ � ����������!���� ....................................................................................... ���������

��& .......................................................................����� ������ �/�����& ..................................................................����� ��� � ����������% ............................................................����� ����� ���'���!��

��& ................................................ JavaBeans������������� �� ��

��% ................................................................................................ � ����� ............................................................................����! ����� ���� .............................................................................................)����

�%� ..............................................................................................�������%� .............................................................. ����������������������%� ......................................................................... � � ��������!��

��� .............................................................. Runnable������� �� ����� ....................................................................... � � ��������� ����

��� ..................................................................................................����

��� ...............................................................................................�������

���................................................................. ��� ��� �������

��� ........................................................................................������ ����� ......................................................................................�� �������& ............................................................................................ �����

Page 16: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� ��

��� ...................................................................... ������� �������$� ................................................................................. �� � ����!�$� ...................................................) ����#� �URL�������� ���$% .........................................................................���������������

�$% ...............................................,JDBC-���'������� � ���� ��������$$ ..........................................................������ ��������������� �&� .............................................................. ������� ����GUI������&% ................................... #�"���#�� �JDBC�� ������ ���� ��� �&� .......................................................................����� �� �� ��

��� ........................................................................................... �� ������% ................................................................................. ������)������$ ............................................................� � �������������� ������& ............................................... �� �������! ������������������ .............................................................. �� �������� ����!��

��� .................................................................... ,JSP-���'���������������� ....................................................................... ��� �� ������������ ......................................................................................JSP��� ���� ................................................................... JSP��������������������& ..........................................................................����������1����� .......................................................... ����/����������� ���� ��� ................................................ ��'���������������������������� .................................................................. ����������� ������!���% ..................................................................................... �����"�JSP

��� .............................................................,RMI-���� ��������������� ............................................................................���� ����� ��� ...................................................................�� ��� ����� � ��& .................................................................. ������������������!���& .................................................................�� ����������� ��

��� .............................................................................................. CORBA��� ............................................................................... CORBA�������� ..............................................................................................� ����� ......................................................... CORBA"���'������� �����$ ........................................................................ RMI�� ���CORBA

��$ .......................................................................... ,EJB-���� ����������

�%& ......................................... ��� ������������ ���JavaBeans�������%� ....................................................................................... EJB���� �%� ...............................................................................��� �����������%� ................................................................... � ������������������%� .....................................................................� �����������������%� ................................................................��� �������������������%% ...................................................................................... ���������

Page 17: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � ��

�%� ............................................................................� ��������������& ...................................................................�����"���� ����������

��& .........................................................................����� ���������Jini��� ............................................................................ )� ��������Jini��� ........................................................................................ (Jini���

��� .............................................................................. Jini�������!����� ....................................................................................������#������� .................................................................................. /��!��#������� ..................................................................................������#������� ..................................................................� � ��� �)����������% .............................................................���� ����� ���������

��� ..................................................................................................����

��� ...............................................................................................�������

� ���..........................................��������������� ���������'�� �"�

� ���...................................... )JNI*���'&����� � ���� ���'�� �"�

� ���.............................................��'&�������� � ���� �'&� �"�

� ���....................................................................... ����� ��'�� �"�

� !��...........................................................................��������� � �

Page 18: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� �����

����������� ��� �� ������������������������������������

�������������������� ��������������������� �� �� ���

� ��� ������������������ ����� ����

���� ���� ���� ����� ���� �� ���� ����� ��� ������������� ���� ��� ������ ����� ����

������������������������������������������������������ ���������

�������������������������������������������������������������������� ���� � ����

����� ����� �� ��� ������� ����� ��� �� � ������ ������� ������� � � ��� ���� �����

��� ����������������������������������������������������������� ��������

� ������������������������������������������������������������������������

� ���������������������������������������������� ���������� ������

�������������������� ������� ��������������������������� ����� �����������

� ���������������������� ����������������������������������������������

� � ������� ��������������������������������������������������������������������������

������������������ ������ ���������������������������������������

��������� �������������������� ����!while!���� ��������������!if!���� �����

����� ��� � ���� ��� ���������������������� ����� ��������� ����������� ���

������������������������������������������������������� �������Perl���� �

��C���������� ������������������������������� �������������������������

��������������������������������������������������C""������������� ���

����������������� ������ ������� ���� ��� ���� ����� �������� �������#����

��������������$�������� ������� ��C���������������������������������� �������

Object-Oriented#� ��������������� ������� ��� ��������� ������� ��� �����

��������������������������������������������������������$OOP���Programming� ����������������������������������������������������������

Page 19: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� ��

���� ����������� �C++� ���C��������������������������������������������

������ �������� ���������� ������������������������������� ��� � �� ���

������������������ ���� ���� ���� ���������������������� ����� ����������

� �C++����C���������������������������������� �������������������������

� � �� �������������� ������� �Thinking in C++� ���� ������������ ��� �������� �

��� ����������� ���� ���������� ������� � ���� ������ ���������������������

������������������������������������������������������������������

�������� ������������������ ���������������������������������������

� �����������������������������������������������������������

� ���������������� ���

����������������������������������� ������� ������������������������ ����

� �����������

��� ���������������� �� ����������������������������� �������� ����

�������������������� ����� ��������������!�����������!���������

������ ����� ������� ������� ���� ���� �� ���� ��� ������ ������� ����

������� ������ ��� ����� ����� ����� ��� ���� ��� ������� ������ ����� �����

������� ��� ������� �� ���� ���� ����� ����� ������ � ��� ������ ���

������������������������������������������ ������!����������� !�

� ��������������

�� ���������������������������������������������� ������������������ ����

� ������������������������������������������������ ������������������

������ ������� ������������ �������� ��� ��� ������ ��� �������� ��� ������� ����

��������%&����������� ������������� ����������������������������

�������������������������������������������� ����� �������������

�������� �������C�������� ����������������������������������

����� ���� ���� ���� ��� ��� ����� ����#� ����������� ��� � ����� ��

������������ ������������ �������������� ������ ��������������$����

������� �� ����� ������ ����� ���� � �� ����� ��� ���� ����� ��

� �������������������������������������

������������������������������ ������� ����������������������� �� ����

��������������������� ��� ������ �������� ������ ����� �������������

� �������������������� ���������������������� ������������������

������������ � �������������� ������ ������ � �� �������� ���� ��� ���� ����

� ������������� ������������ ���������������

Page 20: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � ��

� �������� � Sun� ����� ���� ������ ������ ������ ������ ������� ��� ��������� ������� ����

��� � �� ����� ���� ����� ������ HTML� ������ ����� ����� Microsystems������ ��� ��� ��������������� ������ �� �������������� �������� ��������

���� ���� ����� ����� ������� ������� ���� ��� ������� ������� ���� ���� ��

������ ������������������� ��������������������������������� ��������

�������������������������������� ���� � ������������������������� ��� ���

������� ������� ������#����������������������������������� � �����

�������'������ ������������������������������������$������� �����

� ��������� �������������� ������� �����

� ����� ����������������������������� �������������� ��� ��������������������������

������������ ������������������������������������������������������

����������������������� �����������������������������������������������

���������������������������� ������������������������������� �����������

���� '�� ����� ���� ������� � ����� ��� ������� ��� ����� �� �� ��� �����

� ��������������������������������

�������� ��� ���� ����� ��� �� ���� ������ ��� ��� ���� ���� ���� ��� ����

���� ��� ����� ����� ���� ������ �������� ������� ��� ������ ���� ����� ������

� ����������������������������� ����������

� ���������������������������������������

� !������ � �� ��������

��������� ������������������������� ����������������

��������������!(���������!��������������������

�������� �������� ������ ���������� ��� ��� ������� �����

������������������������������������������ ����������

���� ����� ����������� ����� ����� ������� ����� ��������� �����

������ ���� ����� ������ ������� ���� ����� ����� �����

�������� ������ ��� ��� ������ ����� � ������ ��������� ���

����� ������ ������ �������� ��� �������� ������ ����� �������

������������ ����� � ����� ����������� ���� �� ������

����� �������������������� � ��������������� ������ ��

� �������������������������������� ��

Page 21: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� ��

� !������ �� �������������

�������������������������� ����������� ������������������

������������������������������������������������������������

������������������������������������������������� ������������

�������� ���� � �� ��� ��������� �� ��� ��� ������� ���� �����

������� ����� $���#�� ��������������������������� �� � ��� ���

�������� ������� �������������� ���� ����)����� ���������

� ������ �����������������������static���������

� !"����� � �������� ������� �

��C� ������ ������� ����� ������� ����������� ���� ����� ��� ��

���������������������������� ��������� ����� ������ �C++� ��

������������������������������� ����� ���������� ������

���������������������������������������������������

������� �while���for�������� �if-else������������������������

�break� ����� ������� ���� �continue��� break� ������� �����

����������������� ��������������#�����������continue����� �������������switch����������������$�����������goto������������C++����C���� ���������������������������

���������������� � ������������ �� ��� ����� ������ ���� ��

� �������� ����������������������������������������������

� !#����� � � �������

��� ��� ������� ������ ������ ������� ������ ����� ��� ��

��������������#�������������� ��� ������������� �����

��������� ������� ��� � ����� ��� ����� �$������ ����� ���

�������������� ����� �� �� �� ������ ��������� ��� �����

������� ����� ������ ����� ����� ����� ������ ����� ������

�������� ��������� ��������� ������������������� �� � ��

����� ���� ������ ���� ���� ������ ��������� ���� �����

����� ������ �������� ���� ������ ������� ������� ��� ����

� ����������������������������������� �������

� !$����� ��� ��������

������ ������� �� �� � �� ��� ������� ����� ��� ���� ��� ��

����� �������� ������ ������ ����� ������� ��� �����

���������������import���package����������������������

������ ������ ��� ������� ������ ��� �������� �'���� ���

������ ������ ����� ������ ������ ��� ������� ����� ���� ��

Page 22: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � �"

�����protected��� �private� �public����������������������

���� ������� ����� ��� ������� ��� �!���� � �!� ����� ��� ������

� ������������������������������������

� !%����� ��������������� �

�������� ����� ��� ���� ����� ��� ���� ������� �����

����������������������� ����� ��������������������

������� �$*� �������� ����� ��������������� ��#� ������� ����

������� ����� � �� ��� ��� ����� ������ �� � ����� ����� ���

�������������������� ������������������������������������

������ ������ � ����� �� �� ����� ������ ����� ��� ������

����������������������������������������������� �

��������� �������� ������������������������������ ��

� ������������ �������������� �������������

� !&����� �� ���'��

��$Polymorphism#�������������� ������������������ ���������

������������ �� �������������������������� ����� ��������

� ��� ��������� ����� ����� � ������� �������� ��� ��� �

���� ������ ������� ������� �������� ��� ����� ������ ���

�������� ������ ���� ������� ����� ����� ��������

���������������������������������������������������������

�������������� ����������������������������������

��������������������������������� ���� �������������� ����

� ����������������������������� ����������������������������

� !(����� �� � ����������� ����

���������������������������� ������������� �����������

��� �������������������� �$interface# ������������

����� ������� ��������������������������� �������

������� ������ ���� !����� �����!� ���� ����� ��� ����� ���

������ ����� ����� ������ ���� ������ � �� ��� �C++� ����

� �������������������

�� ������������������������������������������������

� ��� ����� ��� ������� ����� ����� ����� ���� ���

������ ��� �� ��� ���� �� ��� ����� ����� ������� �����

� �� ������ ��� ������ �� ���� ����� ������� ����� ����

������ ����� ������ ������ ������� ���� ������� ����� ��

����������������� ��������� ������������������ �������

� �������������������������������

Page 23: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �#

� !)����� ���� �� ����������

��������������������������������������������������

� ������������������������ ������� �� �� �������������� �

����� �������� ������ ��������������������� ����������

���������� �� ����� ��� ������� ��� ����� �����������������

��������� �������������������������� �������������

���� ������ ����������� ����� ���� ������������������ ������ � �

������� ����� ��� ��� ���� ����� ��� ���� ���������� ��� ���

������� � � �� ������� ����� ������� ������� ��� ��

��������� ��������� ������� ����� ���� ������ ��������

�$ArrayList#������������� �� $������� ���#�������������

� �$HashMap#������������

� !������� � � ������������� ������ �

���������� ��'��������!���������������������������������

������ ������ ��� ������� ���� � ��� ������ �� ��� ���� �!����

�������� ������ ���� ��� ������ ������� ���� ��� �� �����

��������������������������������������������������������

exception#������� ����� ������������������� � ������������

����������������������������������������������$handling��throw� �catch� �try�������������������������� ����������

������� ����� ��� ������� ������ ����� ��� �finally��� �throws��������������������������� ����������� ������ ���������

�������� ��� ���� �� ����� ������� ������ ���� ���� �������

� ������������������������������ ����������������

� !������� �����������*���������

������� �����������������������������������������������

��������� ����� ��� ���� $I/O#� ��� ���� ��� ��� ��� �� �

������� ������� ����� ������� ������ ��� ��� ��� ���

��������������������������������������������������������

������ ����� ������ ��������� !� �!�� !���!� ��� ��� ���� ������

������� � �#� !�����!� �������� ����� ��� ������� ��� ��� ��

��� ����������$��������������������� ����������������

�$object serialization#���������������� ����������������

��� �������������������� ���������������������������������

� �JAR���������

��

Page 24: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � �$

� !������� � �� ����� ��� �� �� �

����������������������������$RTTI#������������������������

������������������������ �������������������� ��������

�������� ��� �� �� ������� ������ ������� �� ��� ����� �� �

�������$����������#�������������� ���������������������

������ �� ����������������� ������� ���������������������

����������������������������������������������� ��������

� ���� ��� ����� ��� ������ ����� ��� � �� ��� ����� �����

����� ��������� ������ ���� ��� ����� ��� ��� ������ ����� �����

�������������������������������� �������������� ���������

� ����������������������������������������������

� !�"����� � ����� ����������

���� ����� ����� �Swing� ������ ��� ������� ������ �������

������������������������� � ���� �������������������������

����� ����� ��� ��� �������� ������� ���� ��������� ��� �����

���� ������� ����������� ���������� ��������� �������� Swing��������������������������������������������������JavaBeans������

� �$RAD#������������������������������������������������

� !�#����� ����� � �� �� �

���������� ����� ������ ������ ������ ���� ������

��� ���� ������� ����� ������ ����� ��� �� ������� ���������

��� ��� � ����� ����� �� ��� ���� ��� ��� ��� ���� �����#

�������������� ����������� $���������� ������ ����� ���

�����������������������������������������������

����'������������������ ��������� ��������������

������� ����������� ������������������������������������

� ���������������������������������������������������

� !�$����� �����������

������������������������������������������������������

�������� ��� ����� ��� ��� �������� � ����� �������� �����

��������� �����������������������������������������

������������ ������������������������������������������

���� ��� �$����� �� ����� ������#� $JSP# �� ����� $servlets#���� �������� �$JDBC# ������ � ������ ���� �������

���� ���������������������������������������$RMI#������

� �$EJB#�����������JavaBeans���������Jini���

Page 25: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �%

� !������� � �� ������������������

����� �������� ��������� ��� ����� � ����� �� ��� ���

������� ��� ��������� �������� ������� ��� �������� ������

���� ����� �������� ������� ������� ������� ����� ������

������ ������ ���������� ����� � �� �� �� ������ �� ����

����� ��� ������ ���� ���� ���� ������� �������� ����� ����

� �����������������������������������String

� !������� +JNI,���������� �����������

����������������������������������� �����������������������

���� ���������������������� �����������������������������

�������������� '����� ����� ������������������������� ��

��������� ���� ������� ������ ������� � �� ��� ��� � �����

��C��������������� ��������#������������������������

��������������������������������������������� �$C++� ��

� �������������� ������������������������� ������

� !������� �������� ������ ����� ���

��������������������������������������������������������

� � �������������������������

� !������� � ����

��������� �������������������������������������������

� �����������

� � ����������������������������������������������� �����������������������

� ������������������������������������������������ ���������

������������������������������������ ����������������� ����������������

���������� ��� ������ ��� ���� �� ����� ���� � ���� � ��� � ���� �����

���������������� ������������������ ������������������� ���������� �

������������������������������������������� ���������������������������

������ ��� �� ���� ��� �� ������������������������������������#� �� ����

� �$�������������������

The Thinking in Java Annotated� ���� ����� ����� ������� ��������� �������

� �www.BruceEckel.com����������������������������������Solution Guide

Page 26: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � �&

� ���������������� ������� �������� ������ ������� ������� ���� ��� ���� ��� ���� �� ��

�� ���� � �� �www.BruceEckel.com� ��������� ����� ����� � �� ��� ����� ������

�������� ��� ������ ����� ����� ���� ������� ���� ��� ������ ��� ����

���� ���� ����� ��� ������� ����� ����� ��������� ����� ����� ��� ���������

���� �$www.BruceEckel.com� ��������������������������#� !���!� ���������

���� ������ ���� ��� ����� ������ � ���� � �� ����� ����� � ��� ����� �����

� ���������������������� ����������� ������'��������������������� ������ ��

�� ��� ��� ��� �� � ����� � ���� ���� �������� ������� ����� ��� ������� ����

������� ��� ��� ��� ��#� ������� ���� � �� ��� ��� ����� ������� �� �����

� $������� ������������������� ���������������������� �������

� ���������������� �������������������� �����'������

//:! :CopyRight.txt

Copyright ©2000 Bruce Eckel

Source code file from the 2nd edition of the book

"Thinking in Java." All rights reserved EXCEPT as

allowed by the following statements:

You can freely use this file

for your own work (personal or commercial),

including modifications and distribution in

executable form only. Permission is granted to use

this file in classroom situations, including its

use in presentation materials, as long as the book

"Thinking in Java" is cited as the source.

Except in classroom situations, you cannot copy

and distribute this code; instead, the sole

distribution point is http://www.BruceEckel.com

(and official mirror sites) where it is

freely available. You cannot remove this

copyright and notice. You cannot distribute

modified versions of the source code in this

package. You cannot use this file in printed

media without the express permission of the

author. Bruce Eckel makes no representation about

the suitability of this software for any purpose.

It is provided "as is" without express or implied

warranty of any kind, including any implied

warranty of merchantability, fitness for a

particular purpose or non-infringement. The entire

risk as to the quality and performance of the

Page 27: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � �� �(

software is with you. Bruce Eckel and the

publisher shall not be liable for any damages

suffered by you or any third party as a result of

using or distributing software. In no event will

Bruce Eckel or the publisher be liable for any

lost revenue, profit, or data, or for direct,

indirect, special, consequential, incidental, or

punitive damages, however caused and regardless of

the theory of liability, arising out of the use of

or inability to use software, even if Bruce Eckel

and the publisher have been advised of the

possibility of such damages. Should the software

prove defective, you assume the cost of all

necessary servicing, repair, or correction. If you

think you've found an error, please submit the

correction using the form you will find at

www.BruceEckel.com. (Please use the same

form for non-code errors found in the book.)///:~

� �������$���������������������#� �������������������� �������������

� ����'������������������������ �������

� ��� ��������������������������������� ��� � ������������������������������ �������� �

�����������#��������������� ���������������Sun�������������

�������� ��� �� ��� ����� � �� ��� ����� �$java.sun.com/docs/codeconv/index.html��������������������������� ���������������������������������������������������

������������������������������� ���� �����������������������������������

� ����� ������������������������������������������������������������

�������� ��������� ��� � �� ��� ����� �������� ����� ��� ��� ����� ��������

��������������������������� ��� ���������������� ������������������

������ �� ��� ���� ������ ��� ��� ����� ������� �������� ��� ��� ������� ���

����������������������������������������������������������+//!+�����������

������� �����������������������'���� ������������������������� ���������

� �$www.BruceEckel.com������������������������#�����

Page 28: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � �� ��

� ��������������� ������ ��� ���� Sun� ����� �� ������� �� � �� � ���� ���� �� ����

� ������������ ��� ��������

����� ����������������������������������������������Sun�������������� ����

����������!� ���"����������� �Sun����� �JDK��������� ������ �������

������ ���� ��� ���� �� ������ ������� ��� #��!��� ���� � �� ����� �$%���� ��&� ���'

���� ��������� �������������������������������� � �� ��!���� �� ����

����� ������ ������ ������ ���� �������� �� ����� �� ��� � �������� ��� �� �� ����

� �$�!���������������� ��������Linux��������������!����������� ����Linux

��� ��!����������������!������������ ��� ������������ ������������

���!�� #��� �� ��"����� ���!� � � � �!�� �� ����� �� �������� ����� ��� ���

� �www.BruceEckel.com���� ����� ���������������������

��!��� �� �� ���� ������� ��� �� ����� ���� ����� ����� ����� ������� ���

����������������������������������� ������!� ����� ��������!�������

�����!������� ����������!�� ����������!�������� ��� ��������� ��������

� ���������

� ��� ������������!��� ��� ��� �� � ���� ��� ��!��� �� ��� �� ���� ��� �� � #��� �� ��"����

������� ����� ���� ��� �������"��� ��" ��!� �!� �� ���� ��� ������� ���� �

���(�������������� ��������� ����� ���������HTML�" ��!������� ��

� ��!������������� ������������������������������������

�$�������� ������ �"� ���������� � ��"����� �� � �!���!��� ����� ��� ���

�#����� �Chuck Allison� �� � Thinking in C: Foundations for C++ & Java� ���

�����������������C++���!�������� ���������������������� ���������!�

���� �� � ����� �� ����� ����� ����� � ���)� ��������� ������� �C� �!�������

�)������� C++�������������C��!���������������������C++�����������C��!�

�C��!����������������� ��� ������������"������������ �����������

������ �����������������!����!����"�����#��������� ����"����� �����

����C�������������� �������������C*C++���!� ����������������������� �

� ����������������������C++������������

������������!����������"������!������������MB����������������������

� �$Java2SDK��������������(���������Java 2 SDK��Sun

Page 29: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� �

� �������������

���� � ���� ������������������ �������� ���� ��

� ������������������������������ �������

����� ������ �������� ���� � ���� �� ����� � �� ���� ���� ���� ������� ��

������������� ������ �������������������� ������Steve Jobs������ ��

����� �� ��� ���� ��� �������� ������� ����� ��������������� �

�������� ��� � � ������ ������ ����� � ����� ������ ������ ����� ������� ���

����� � � �OOP� �� Object-Oriented Programming�� ������������ � �����

� ����������������������� ������ � ���

����� ��������� � ��� ��� � ��� ������� � ���� � ��� ����� ����� � ���

������ ����� ����� ��� ���� ������ � � ���� � � ���� ������ ����� ��� ����

����� �������C���������������������procedural programming����� ������

���� ���� ������� ����C������������������������ � ���� � ���

Thinking in C: Foundations for C++ &� ������� ����������� ������� ����

������ �� �� ����� ����� !��� � �������� ��� � �Chuck Allison� ���� Java������� ����������� �������C� ������������� ��www.BruceEckel.com�����

� ������ ���������� ��C����������

����� ���������������������������������������������������� ����

����� ����� ����� � �� � ��� ���� ���� ��� �� ���� �������� � ��� ���

�� ��� ��� � ��� ������ ����� ��� ��� �� ���� ����� ����� ���� ����

������������� � �� ������������ ��������������� ���������������

���� ���������������������� �������� ���������� ��������� �����

���� �� ������ ��� � ����� ���� ������ ���������� ������ ����� ����

����� ���� ��� ��� � �� � ���� ��� ����� ����� ��� � � �� � ��� ����� � ������

� �

Page 30: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

����� ���� �� ���� � ���� "���� ��� � ��� � �� � ��� ����� �� ����� ����

������ ������ ��������������� ���� ��� �� ������� �� ���� ��� ���� ���� �

� �� �������� ������

� �� ���������������� � ������� ����� ���� ��abstraction�� ���� ������ ��� � ���� ��

��� ������ ����� � �� ������ ��� ����������������������������

���� ��� ��� � �� � �assembly language�� ������ #������ ���� ��� � ��

imperative�� ����� ����� ��� � ���� ���� � ����� ���� � ���� � �����

������������������ ����������������� ��������� �������languages���������������� ����������!������������� �� ��C��BASIC��Fortran���� ������ ����� ���� ������� ������ � ��� ������ � ��� � ���� �!� ����

���������� ������� ��� ���� ��� � ���������������������� � ��

� �� ��������� �� �������� ��������� �� ������������� ��������

��������� ����� ���� � ��� � ���� ���� ������� ������ ��� � ��� � ��� ���

�� � ������ ����� "��� � �� ���� ��� � �������� �� ��� � � �� �� ���

������ ������ ���� � �� ����� ��� � ���� � ����� ����� � �� ���

� ����� ����������������������������������� � ���

������� ��� ���� ��� � ��� ���� ������ �� � � �� � ��� ���� ������� ���

����� �������������� ����� �������������APL��LISP�������������

�PROLOG������� ��� ��������������� ����� ��������������������������

������ ���� ��� ��� ����� � ��� ������� ���� ���� � ��� ��� ����

����� ����� ��� ������������������ ���������������������������������

��� �� ���� � ���� � ���� ��� ����� �� � �� � ���� �� ���� ��� ������ ������

� ���������� �� ��� ����� ���� ������ �������������� ��

�������������������� ������ ���������������� ��������� ������ ���

������ ������� �� �� ����� ������ � �� ����� � ��� �������������� �����

��������� ��� � ������� �������� ��������� �� � ���� ��� ���� ����� ���

����������� ��� ��� �� ������� ��objects�� ���������� ���� ����� � ������ � ��

����� ������������ ��� ����� ��� ��� ��������������� ������������

���������������������������� � ������� � ��� �������� ������ ������

��������� ����������� �������� ������������ �������� ������ �����

��������� �� ���������� �� � � �� ����� ������� ��� �� ���� ��� ���

���� ����� �� ��� � ��� ����� ��� ������ �������� � ��� ��� � � �� ����

������������ ������������� �"��� ��������� ������������������������������������state���� ��������������������� ������������������ ���

������ ���������� �� �� �� ��� ��� � ��� � ��� ������ ��� ����� ����

� ���� � ������������� �����������������

Page 31: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

����������������������������� � ������ ������ ��������������

��������������������������������� � ���� ���������������������

� �$��Multiparadigm programming languages������� ������ �����������

������ ���� ����� �Smalltalk�������������������� ����������Alan Kay�������� ���� ������ � ����� ��� � ���� ��������������� ��� �� ���

� %��������� �������� � �� ������������ ����������

������� � � ������� ���� ���� ������� ��� ���� ��������� ��� ���� ��� ����

���� ����� ������ ���� � � �������� ������ ��� ��� ���� ��� ���� ����

�������������� ��� ���� ��� ��������������������������� �� ������

� �����������������������������&���������������

����� � ���� ��� ������ � ��� �� ��� ��� ���������� ��� ����� ��� ������� ����

������������������ �� ������ ������������ ������� ������ ������

�������� ����������� ���� ���� ���� ����� ����� �� �� � ��� ����

� �������������

� ��� ������ ������� ������� ��������� � ����� ��� � ������� ��� �������� ���� ����

��������� ��������� ���� � ����� ������ ���� ��� ������� ��� ���� ��� ���

������������� ���� ���� ������������������������� � �� ����

� ���������� ���

���� �instance�� ��� � � � ������� ��� � �� � ����� ������� ��� �������� ���� ����

���� � � � ������ � �������� �� ���� �� � � ������ ����� ��class�� ���

� �# ����������������� �� ���� � ������������

� ��� � � � � ������� ����� ��� ����� ������� ���� � ����� � ���������� ��� ����

�������� ��� � � ������ ���� �������� ����� ���� �� ������ ���� ������

������������������������ ��������� �������������������������

�������� ���� ���� ������ ����� ������� ����� ���� ��� ������ ��

� ��� ��������� ������� � � �substitutability����������� � �����������

� ��������� � �������

� %������ ������������������������������Booch

� ��identity�������behavior����������state���� �����������

���� ��� ����� ����� ���� ��� ����� ����� ��� �� ���� �������� ���� ��� ����

���������� ��������� ���������������� �� ��������������������� �

� '����� ��������������������������������������������� ����

Addison���� ��Timothy Budd�����Multiparadigm Programming in Leda���� � ���$

� �Wesley

�������������������� �������� ����������������� ���� � �������� ������ � ��� �'

����� �������������� ����� �� �������������������������������������������� ����� ���������������������������

Page 32: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

� � � ����������������� ��� ������ ����� ����� ���� �� ���� � ���� � � �Aristotle�� ����������� ���� � ��������� ������ ����� ������� ��� ����� � � ��type�� �����

����������������� ��������������� �������������� ��!������������

� ��� � ��� � ����� ����� ����� ��� � �������� ��� � � �������� � �

�class� � ��� ������ � ���� � ������ �Simula-67� ��������� ������ ��� �

� �������������������� �� �������� �����

� ��� � ��� �������� ����� ���� ��� � ���� ������� ���� �Simula� ���

������� �������� ��� ���� ������ � ����� ������ ���� � ������� ������

�� �� ��� ���������������������� �� ���!�����������������������

������������ ���������� ���� ����������� ����� ������ �� ������������

������ �� � ������������������� ���������� �class� ���� ������ ��� ����

������������������������������� ���� ��������� � �������� �����

��objects����������������� ������������������������������%�����������

������������������� ���������� ������ �������instances������� � �

�� �� � ���� ��� ��requests�� ������ �� �messages�� ������ ���� � ����� �

� ����� ���� �members�� ����� � ����� � � ��� ����� �� ������ ������

�� ��� ������������������ � ����������������%��������������������

�������������� �� ����������������%���������������������� ���������&�

������� ������ ������ �������� �������������������������� ��������

�������� ��� ������� � �� � � ���� ����� � ������ ������� ���� ���� ��� �&�

� ������� � �������� ���� ����� �� �� ������������

����� ����� � � ���������� ��� ����� ����� ����� ��� ��� ����� ���

������� ������� ����� �������� ����� ��� � ���� ��� ������� ���

� (��� ���class����������������type� ��� ���� ��� ������class�����

�������� �� � �������� � �� ���� ��������� ��� ���� ������ ������ ����

���data type�� ������� ���� ������� � ����� ����������� ���� � � ����

�� � ��� � ���� � ��������� ��� ������� ������ � ��� ��� �������� ���

����� �������� ��"����������� � ��������������� ����������������

����������� �������������� ���������������������������������������

���� �������������� ������� ��������������� ����������� �������

��type checking�������������������� �������� �� ��������� ������

� ���������������������� �

����������� ����� � �������������������������������������� ����������� ����(

� �����������

Page 33: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

� ������������ ��������������������������� ����������� ������ ���

������������ � ������� ����� ��������� ���� ��������� ���������

� ������������������� ���� �������������������������������

� ��� ����� � ������������������������������������ ����� ��� ������

������� ��� ���� ���� ��� ������������ ������ �������������������

������������������������ ��������� � ������ ���������� ���������

� ����� ���������������� ������ ��� ��������������� ����

���� �������� ��������#������� �����������������������������������

������ ���� ������������ �������� ������ ��� ����������������� ���

����� � ������ ������ ����� ���� ����� ���� ������� ��� ����� ����

�� ����� ������interface���� ������������������������� ����������

� % ������������ ���������� ���� ��������� ��������

�����

�����

������

����� ���

�����

Light lt = new Light();lt.on();

������ �� ����� ��� ���� ������������������� ��������� ����� ��������

� ��������� ���� ��������� ����� ���� ������������� ��������

������������ ����� ���� ������� ��������implementation���� � ���

� ���� ����� ����� �������� ���� ���� ������ ����� ��� ������ ������

����� ���� ����� ������ � ��� �� ������ ����� � ���� � ��������� �����

��������� �������������� � ������� ���� � ���������� �� ����� ���

� ����������� �� ��

����� �Light�������������������Light�� � ���� )���� �������� ����

����� ��� �Light������������������ ��������������� � �lt�� � �����

��������Light������������ ���������������������� �� ��������� ���

����� ������new����� ������� ����� �It������������ �reference����������

������������� ��� ������� ���� �� ���������� � ��������������������

���� ��� � ����� � ��� ������� �� � ����� ��� ��� ����� �������

� ���������������� ������� �������������� �����������

� ��������

� �

Page 34: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

������������Unified Modeling Language��UML������������������ �������

�� ��� �������� � �������������� ���������� ������������ ��������������

member����������������������� � ����������������� ��� �������������

���� ���� ���� �� � ���� ������ � �������� ����� � ������ �� functions�UML���������������������������� ��� �������� � ������������������

���������� ���� ������������� ������� ����� ������ � ���� �������

� ����� ���� ���������� �� �������� ���� ��������������� ��

� ������ ��������������� �����class creators������� ����������������� �����������������

��������������� � ����� ��client programmers�� *����� ����� �� ����������

������ ����������� ���� ����������������� �����������������������

� ����������� � ���� �������������������������� ������������������

�#���� ������� ������ ������������ ���� �������"��� ����������

��������� ���� ������ ������ ������������ ������������ ��������

���� � ������� ��� ���� �� ������� �� ����� ���� � ����� ���� ���� � ��� ���

��������������� �� ������� ��������� ������� ������������� ����������

������ � ����� ��� ������� ���� � ���� � ����� ������ ����� ������ ��� �� � �

� ����������������� ����� ����� ������������

�������� � ����� � ��� ���� ��� ������ ����� ����� ���� ������� ������ ���

�������� ��������� �������������������������� ������������� ����

����� ���� ����������� ���� ����� ����������� �������� ��� �����

� ������ ���� ����

���� ����� ����������������� ������ ���������� � ���� �����������

������� !����� ��� � ����� ����� ������� ���� !���� ���� ��� ���� � ����

������������ ��������������������� ���� ��������������������������

� ���� ����!����� ���access control�����

���������� ����������� ������ � � ������������ �� ��� � ��� � ������

����� ����������� ������������������������ �� ������� ����������

��������� ���� ������ �������� ������ ������������������ ������

������� � � �� �� ���� ���������� ����� ����� � �� ��� ����� � ����

� �������� ��������� �� ������� ������� ��������������� �

���� ���� ���� � ������ ������ ���� �� � � ���� ����� ���� �� ��� � ���

�� ��������� ������������ ��������������������� ���� �������� ����

� ������������������ ������ ��������������������� ����������������

� �� �������Scott Meyers �������*

Page 35: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

������������ ����� ���� ������ ��"��� ������� ���� ��������������

� �������� ���� �����������������

����� � ��� ��� �� ���� ������ ����� ����� ����� ����� ��� ������ ���

�� � �� ������ ������� � �� ���� � �protected�� private� �public� % �����

���� � ���� �� ���� �� ���� ��� ������ ��� �access specifiers�� ���� �����

���������� � ���������������� �� �������������public� ��� ��� ����

�������� ������������������!��� ������������private����� ������� ����

� ��������������������������������� ���������

� ��� ����������� ������������������������� ���private����� �����

�����������������protected� ��� ����� ��� �� ����������� ����������������

�������� ��� ��� ������ ������� ����� ���� ����� ������ ���� ���� �private� ����� ������� �� �����������

�!����������������!��������� ������������������������������������

���friendly������������ ���������� ��� � ������������ �������� �������

������ ��������������� ����������� � ������������ ����� ������ ���

� ��������������������������������� ������"��������package�

� ��������� �� ����������� �������������������������� ������ �� ���� ��� ����������

����� �� �� ������ ���� ��� �reusability�� ����� �� ���� ���� �� ������ ���

��������������� ��� ������ ������� �� ���� ��� ����������� ����������

����� � ���� � � ���� � �� ����� �� �� ����� �� � � ���� � � � �� ���

� ����������������������������������������

���� �������� ������ ���� �� �� � � ������ � �� ������ ����� ��� � ���

��� � ���� ��������� � ������������������ ������������ � ����� ��

������������� �� �������� ��� � ���� � ���������������������� �����

���� ��� �� ���� �� ��� ��� ����� ���� ������� ��������� ��� ����� ����

����� ���� ����������� �������������� ��� � ������ ��� �������

������ �������������������composition������ ��� ������������������

��has-a�� �������������������� ��� ���������������������� ��aggregation�

� ���Engine���������Car��������� ���������������

��� ������

Page 36: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

�������� ����� � ���� � ���� � ����� ������� ��� � �� ����� UML� ������

������ �������������������%���� ���� ������������������� ���������

� �+������� ���

�� � ���� ��� � ������ ����� � �������� � � ��� ������� �������� ���

�������� � ��� � ������� ������������ ���� �� �� �� ����� ����� ������

��������������������������������������������������������� �� �����

��� � ����������������������� ������ ������� ��������� ����������

������ ����������������� ������� ������� ���inheritance�����������

�������� ���������������� � �� ������ ���� ������� �compiler���� �

� � ��

����� �� ���� ������ ��������� � ��� ����� ������ ���� �� �� ����

����� �� ������ ����������������������������������������� ��

����� �� ��� � ����� ����� ����� ����� ����� ��� �� ���� ��� � ����

��������� ����� ���� � ��������������������������� ��� ������ �

�������� � ������������������������������� � ��������� �� � ��������

� � �� ������ ���������������

� � ������� �� ��� ����������� ���� ���� ������ � � ��� ���� � � ������� ������� ��� ����

������ ��� � ������ ����� ����� ������ ����� ��� ������� ��� ����� ��� ����

�������� ��� ����� ����� � ����� ����� � �������� ���� �� "�����

� �� ������class����� ���������������������� �������������������

���������"����� �� ��������� � ������������������������ ��� ��� ���

���� ����� ���� �� !���� � � � ��� ������� ��� �� ����� � ���� ����

������� ���� ������ ���� ��������� �� � ��������������� � ����

���������������������inheritance��������� ������ �������

��� ��base class� ����������� � �� ������� � ���������

��� � ��parent class� � ��� ���� � �� �superclass� � ������ ��� �� �derived class� �� ������ ��� � �� �� ���� � ��� �

��� �subclass� �� ��� ��� �� �inherited class� �� ������ ��� � ��� ������ ����� ����!�����child class�����������

������� ��� �� � � ���� � ��� ������ ����� UML� ������� "� �

� �������� �� ������������ �� �������� �� ��������������

� ��� ���� ��� ������� ������� �� � ������ �������� � ���� ������ ����� � ����� �����+

� � ��� ���� ����������� ������ ����

����

������

Page 37: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

��������������������������� ����������� ������������������ �������

���� � � �������� �� �� ������ ������� ���� ������� ������� ��� �����

������ ��� ���� ��� � ��� �������� ���� ��� �� ���� ���� ����� ��� �������

� ����� ���������� �� ��������� ������������� �����������������

derived������������������base type��������������������� ������������������

��������� ������ � ��� � � ������� � ��� ��� ����� ���� � ����� ��type������ ���� ���� � ��� ��� ������ ���� ����� ����� ���� ��� ���� ��� ��

�������� ���� ����� � �� ��� ����� ������� ����������������������������

� � ���������������� ���� ������ ������� ������������

���������� ������ ����� ������� ���������������� �� ��, �� ���� ����

���� ��������������������� �� ��� ����������� ��&���������������� ���

������������������������� ���� ���������� ������������������������

����������� � ��������������������������������������� � ��������

���� �������������������������� ���������������� � ������!��

� ��� � ��� ������� ������� ��� ����� � ���� ���� � �� � �� ��� ������

� � ��������� ��������������� ��� ���

��� ����������������������������� �������� ������ �������� � ���� ���

��������� ��� ���� �Shape�� � ����� ����� ������ ��������������� ������

������ �move()� � �� �erase()������ �draw()������� ���� ������ �&�� ������ ����

���������Circle��������%���������������������� �� ������ ��������&���setColor()���� � � �� �� ������ ���� ���� ����� � �� � ��� ��Triangle�� ���� � ��Square����������� � � ��� ����� ��������������� � ����� ��� �������������

������� � � �� � ���� ��� ���� � ���� ����� ��� � ���� ���� ��� �� ����

� %��� ���������� ����� ����� ����� � ����������������

�����

�������

�������

������

� ���������

����������

���� ������ ������

� ���������� ������� � ����� ���� ������ ����� ��� � ����� ���� �����

������� � ���������� ��� ����� ����� � ������ ��� � ������ ��� �� ���

������ ����� � ������ ������ ���� ���� ��� ������ � ��� � �� � ������

Page 38: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � �

���� � ��� ������ ���� ���� � ���� ����� ����� ����� � ������ �����

����� �����!� ���� ��� ����� ����������� ��� ��� ��������� � ��

� � ������ ���������������� ����������������������������������

���� ��� ����� � ���� ����� ����� ����� ���� ��� ������ ������ ���� ����

������ ���� ��� ������� ������ � ����� �� ������ ���� � ���� � ��� �����

������� ������ ����� �������������� ����������� � � ������������ ���

���������������������������� ���������������������������������

���� ��� � ���� ���� � ����� ��� ������ ��� ���� ��� � � ���� � ��

����������� �������� ��� �� ������� ������������� � ��������������

����� � ������ �� ��� � � ������ ����� � ����� ������ ������ ���� � ��

���� ������ �� �� ������ � ����� � ���� �� � � �� � ������� ������

� ���������� � ������

��� ������� ������ �������� � ��� � � ���� �������� ����������� ���

��������������������������� �������������� �������������� ��������

�������� �����������!������� ������� ����������������������� ��

���� ���������� ����� ��� ����� ��� � � ������ ������ ����� ���� � �����

��� � � ��� ��� � �� � ��� ���� ������ ���� ��� � �� ��� �� � � ����

� �������������������� ������

������� ����� ������������ ��� ��� � � ���� ��������� �����������������

���� � � ������ ����� ������ !���� ���� ��� %����� ���� ���

����� ��� ����� ����� � ������ ��� ���� � ��� ���� ���� ��� ����� �����

���������� ������ ������ �������� ����� ��������������� �������

������ ��������� ����� �������� �� �� �� ��������������������

����������� ���� ���� ����������� ������������� �������������� ���

�������������� ���� ��� � �������� ����� ���� ��� � ��� �� �������

� ���������� �������

�����

�������

�������

������

� ���������

����������

���� ������ ������

���������������

�����������������

Page 39: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� �!

�������������!�� ������� ���������������� ���� ���� �� ������

����������extends��� � �� ������������ ������ ����������������������

� ��� � ���� � ������ ���� ��� � ��� � ��� � ������ ��� ��� ��� � �����

��� �� ��������� ���������������� ���������� � ������������ �����

� � ����� �������overriding������� ��

�����

����

������

�����

���������

���������

������

����

������

��� ��

����

������

������

����

������

���� � � ������ ����� ���� ���� ��� ��������� ���� �������������

� ������ �� ��� ��� ��� ������������� ���� ���������� ���� ����� ��

� ��������� ����� ����� �� ��

� "���#"� �������$�" �%������"� ������������������� ����� �� ��� � % �� ������������������ �������

�������� ������ ����� ����� ����� ������ !�� �� ���� ���� � ������ ��

������� ��� ����� ���� ������ � �� � � � ���� �� ����� ��� ����� #�����

���� ������� !��� �� ���� ����� ����� ������ ����� ��� �� � �� ���� �����

����� ����� �� � ���� �� ���� ��� � � ���� � ��� �������� ���� � �����

�� � ������� �������substitution principle������������������ ������

������� ��������� ������� ����� ����� � ����� � �� ���������������� ����

������������������������������������� ���is-a����������������������� �

���������������� ���� ������������� �� � ���a circle is a shape��� � �����

� �������� ������������ ������������� ������

����������� ������� �������������������������!�� ���������������������

������ ��������� ����� �����!��� ������������������������������ ���

������ ���������� ������������� ������ ������������ ��� ��� ���

����� ������� �������%��� �����is-like-a�������� �����������������������

����������� ������ ����� ��������������������� �������� ����� ���

������������������� ������ �� ������ ���������������������� ��� ����

Page 40: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

����!����� ������������� � ��������������������������������������

� ������ ������� ��� ����� �������������������������� ����������

��� � ������� ������ ��� ��� ������ ������ ��� � ������� ����� ����� ����

������� � ��� ���� � ���� � ������ � ��� ���� � ���� ��� ������� �����

� ����� ��������������������� �������� ������ �������� ����

�������������

�������

���������������

�������

���������

�������

�������

���������

������������������

�������

��������� ����� � ���� � ������� ��� ������ ��� � ��� � ��� ������� �����

��������� ����� ��� ���� �� ��� ������� ������ ��� �Cooling System�� �����

����� ��� ������������ ������������������������������ �����������

� ������ ��������������� ������� ��� ����� � ���������� ���������

� ���� ���� ��� �� � �� ��� �� � �������������� ��� ���������������

� �������� � �� ��������������� ��� �������� � ���������� �����������

���� ������ ����� ������ !�� �� ������ ���� ��� ���� � �� ������ ����

� �� ������������ �������������� ������� ������������ �� ����

� ������������������������������&'����������� ��� �������� ����� �� ���� ���� ���� �������� ��� ������ �� ������

����������������������������� ��������� ������������������� ����� �

����� ������������������������������ ���� ������� ���������������

�������� ��� ����������� ��������� �������������������� ��������� �����

� �� ���������� ���������� ��� � � ������� ������� ������� ������

� � �� ���������� ��������� ����������������� �� ����������

���� ��� ����������������� � ����������������� ���������� ����

����� �������� � ���������������������������� ��� ������� ��

�������� �� � ������������������� ������� � ����� ���������������

���������� �� ������ ��������������������� ������ �����������

���� � ��� ������ �� �� ���� � ���� ������ ����������� ��� �� ���� ��� ����

� � �� ��� ���������� �������������� ����

�� �����

Page 41: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

�������� ��� �� �������� ��������� ��� ����� ������� ���� ������� ��� � ��

���&������������������������������������������������ ��������� �����

����������������������� ������������������� ��������������� �������

������������� ������ ��� ���������������� � �� ����������������� ���

�������� ��������������������� ������� �� ���%���� ���� ����������

� ��������� �� ����� ������ ��� ���� ������� ��� ��� �� ���� ������� ����

������������������� ���������� ������� ��� ��������������� �� ���

����� ����� � �� �� � ������������� !������ ������� ��� ���� �������� ����

���������� � ������ ������ ������ ������������������ ��������� �

���� �������� � ����� # ���� � �� ���� ������ ��������� ������� �������

�Bird����� ����� �����������������������������- �. ���BirdController�������

���������������������� ��� ������� ����� � ���������� ������������

�������� ����� ����� ���������������������������� �������������

������ ������� ������ ������������ ��� � ���� �� ������ � ��������

������ �������� ������������ �������� ���move()� ���� ��������� ��

� #� �����"������������������ ������!���"�� ��� � ��� � �������

�����������

��������� ��

������

����

�����

�����

�����

����

�����

�������������

���������

��������������� � �%�������� � ������������� �"�� ��� ����� ���

����� �� �� ���� ��� ���� � ������ ���� � ������ � ����� ������ ����

�������� ���� ��early binding������ ������� ���� ����������������� ��

����� ��� ����� ������ ������ ��� ����� ��� ������ ���� ����� ����� ���

����������linker����� ������� ��������� �����������compiler���� �

���������� � ��� ����� �"�� ������ �� � �������� ������� � ����

����� ����"�� ���� � ��� � �� ������� �������������� ���� �������

� �������������� �� �����������

��������������������������������� �������� � � ��� ������������

������ ���� ����� �� � ��������� �� � ���� ����� ��late binding�� ���� ��������� ������ ����� ������ ���� �� ������ ���� �� � � � ��� � �� �� ����

� ��� ��� �� � � ��� ���� � �� � ��� � ��� ������� � ��� �type checking�

����� ��� � � ��� ��weakly typed� �� ������ � � � � ������ ����� ������ ��

� ���������������������� ��

�������������

� �move()��������

Page 42: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

� �����������dynamic binding���� ������������� �� ��������������������

�!��������� ��������� ����������� ����������������������������

���/������ ��� ������ ���� ���������������� ���������������� ����

�� ������������������� ����� �������� � ��������������������� ����

� � �� ������������� ������ �� ������� ���������� �� ����� ����

���� �� ��� ������������ � ������� ��C++�����������������������

�������������������������� ����� ���������� ������ ���� �����

�������� ����� ���� ������� ���� ��� � ������ ������� ����� ����� ������ ����

�� ��������������!�� ����������� ����� �������� � ���� ����� �����

� ��polymorphism��������������������

������������ ������ ��������� ������� ������� � �� ������������

��������������������� �������� ������� ���������������������� ���

����� � �������������� ������������������� ����� ������������������

�� � ��������� ��������������� ����������������� ����� ���decoupled�

������ ������ �� � � �� � ���� ��� � ���� � ����� ������������� !���� ��

� ����� ������� � ������ � ���� ����� ���� ���� � �� � ����� ���� ��� ���

� ���extensible����������������� �����

� %������������������������ �����������

void doStuff(Shape s) { s.erase(); // ... s.draw();}

������� �������� ������� ���� ����� ������ ��������������� � ����

� ������ ������ ���� ���� ����� ����� ��� ������ ������� �� �

� %�� ��� �����doStuff()

Circle c = new Circle();Triangle t = new Triangle();Line l = new Line();doStuff(c);doStuff(t);doStuff(l);

�������� ��������������������������� �� �� �����doStuff()��������

� �������

� % �� ���� �������������� �� ��� ������� �� ����

doStuff(c);

Page 43: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

���������������������� ���� ��� � ������������������ ����� ���� �

��������������� ������ � ���������������� �� ����doStuff()� ���� �� ��

� �������� �������� � � ���������� ��������� ����doStuff()��� �� ���

����� ���� � ����� � � ����� � �� ������ ������ � ��� � ��� ��� ������ �

����� ������ ��� ����������� �casting�� � � ��� � ��upcasting�� �� ��� ������ � �� ���������� � ����� � ����� ��� �up���� �� ��� � ������

� �� � ����� � ������ ����� �� � � ����� � ����� ����� ���� � �����

� ��� ��� �������� �� ��������� ��������� ������������

�����

��� �� ������ ������

���������

���� � �� ���� � ��������� ��������� �� � �����������������������

������� ��������� �������� ����� ���� ���������������������� ��� ��

� %doStuff()���

s.erase();// ...s.draw();

���� ���� ��������� ���������� ��������� ������� ��������������������

� ���� ������� � ������ � ��� ��� ����� ���� ��� �� ��� ����� ��� �� ��

��������!������������������������� � ������� � �� �������� �� ���

������ ���� ���� ���� ��� � ��� ���� ���� ���� ��� ������ ���� � ��� ��

� ������������������������� � ���������������������

� ����� � ��� ���� �� � ��� � � ������ � � �doStuff()� ��� ���� ������� �

�� ����� ����� "��� �� ���� ����� ����� ���� ��������� draw()� �����

�������� ���������draw()���� ����������������������� ����� ���

���� ������ � � �� �������� ����� �������� ��������� � ��� �

����� ��doStuff()������� ������ ���������� ��������������� ���������

�������������� �� ������� ������������������������������������������

���������������Shape����� �����������������draw()��erase()����������

��� ��� ����������� �� ���� � �Line� ��Square� �Circle������� ������

� ��� ������������������������������ ��� ��� ��������� � �����������

���������� �� � ���� ����� ����� ���� �� ����� ����� � �� ���� � ��

� � ����� �� ���������������� ���� ���� ����������

� ����������

Page 44: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

� ��� ����� ���������������������������������� �������� ��� ����������� �����������������

�������������������������� ����� �������������� ����� � ������

�������� ������ � � ������������� �� ���� ����� ���������������� �����

� ��������abstract����� ����������������abstract������� �� ���� �����

����� � ��� ������ ������� �� � � ������� ��������������� ����� ��

� ����������!���������

��� ������ ���� ���� ���� ����� ���� abstract� ���� � ������ ���� �� ��� ���

�� � ��������� � ������� ��������������������� ����� � �����������

� ������������������ ������������� �� ������������������� ��������

� ���� ���������� � ��� �������������� � ��� � �������������������

������� ������� ������������������ ���������� ���������� ������

� ��������������!�� �������"���������

����� ���������� ����� ��� ���� ��������� ������� interface� ���� �����

����� � � ���� � ������� ���� �� ����� � ��� ������ ���� ���� ��� � ����

������ ���� ����� � ��� ���������� �� ���� ������� ���� "�� �����

� �� � ����� � ���� ��� ����� ������� ����� ����� ���� ����� !��� ������

� �������� ����������������������������

� ���������� ������(������������ �� �� �� �������� ������� ���� �� ���� �������� � ��� ��� � �����

� � !���� ������ ����� ��� ����� � ����� ����� ������� ��� ����������

� �������������

���� � �������� ����� ��������� �� ��� � � � ������ ����� � ����� � ���

������ #��� ���� � ����� ����� ���� ����� ������ � ��� ��� � �����

� ��� ��� ������ ���� �����������C++��������������������������

�� ������ �� ��������� ��� ��� � ����� ������ ����� �� � ���� ������ ���

���� � ���� ��� ����� � ������ �� �� ���� � ���� ���� � ���� ��� ����� ���

��automatic������� ���������������������� �����stack������� ����������

������� ����� ��� � ����� � ���� � ����� �� ��scoped� �� ��������� ������ �

�������� ���������� �� ���� ���� ���� � ������ ���� ����� ���� ��

���� ����� ������ ���� ������� ��� ���� ����� ������ ��� ��� � ��� �������

������ ��������� �������������� ���������� ����� � ����� ���������

��� ������� �� �������� ��� ���� ���� ����������� ��������� ��� �����

� �������������� �������� ���������

Page 45: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

���heap�� ���� �� ����� ��������������������������������� � ��� � ���

��� ������� ����� � �� ������ ����������� ���� ��� ��� �������������� � ����

������ �� ��� ��� � ����� ���� ������ ��� ������ �� ��� ���� � ���� � �� �

������ ����� ������ ��� ���� ���� ��� ��������������� � ������ �����

��������� ����� ��� ������ ��� ��� ������������� ������ ����������

�������� �������� ������ ��� � �� � ����� ������� ����� ���� ������ ����

������ � ������� �� !����������� �� ���� ���� ����������������� ����

����� � � ������������ � ��� ��� ����� � ���� ������ �� �� ���

������ ��� ������� ����� ��������������� ������������������������

������ ������!����������������������� �� ��� ������ �� ��������� ����

� ������ ���� ���������������������� ��

�������������� ��������������0����������� ��� � ���������� ������������

� �������������������������������������new����� ������������

����������������������������� ��������������������������� ����� ���

������ ��� ����� ��� ����� ���� � � ������� � ���� ���� ��� ��� � � � �� �

����� ����������������� ��� � ������ �������������������� ����������

���� ������������ ����������������������������������C++������� �������

���C++� ������ ��� ������ �� �� ���� � ���������� ���� �������� ��� �

������ ��� � ��garbage collector�� ���� ��� � �� � ���� ������ ������ ���

�� �� ���� ��� �!����������������������������������������������

��������� ��������� ����������������������� ���������������� �����

���� ����� � ��� � ��� �� �� ��� ����� ����� ��� � !���� � �� ���� �����

� �������� ����C++����������������� ��� �� ��������� ������

�������� � ����� ���� ����������� ������ ������ ���� � !���������

� ���������

� �������������������� � ���������� ������� ���������������� ����������� ��������������

������ ��� ��������� ���� ������ ������������� ������� ������ �� �������

����� ��� � ����� ���� ��� #��������� ���� ���� ����� ���� ��� ����� ���

� � ��� ��� ������������ �����

����!����������� ���%�� �������������� � ������������ ���������

�������������� ��� ���� ������� ������� ���� ������� �������������

����� ���� ���� � ����� � �� ������ ��� ��� ���� ����� ���� ������ ������

�� ���container���� ��� !����� �������� �� ��������������� ���� ���������

�������������� ������������������������������collection��������������

� ������ ����� ����� �������� ������primitive types��������� ���� �����0

Page 46: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

������������������ � ��������������������������� ��������� ����������

��� � ������� ������������ ����������������� ������������������������

� ���������������� � ������������������� ����

������ ��� !��� ��� ����� ��� �������������� ���� ���� �� � � ����

�� � �C++� ������� � ���� ������� � �!��� �C++������ � ���� ������

���STL� �� Standard Template Library�� ������� � ���� � �������� ������ ��

Visual�� ��� ������ � ������ � �������� ����� ����� Object Pascal� ���

���� ������ ��� ���� !��� ����� Smalltalk� ���� ��VCL� �� Component Library� ���� ��� � ����� �� ����� � ��� ���� � ������ ����� ����� ������ ���

������� ������� ������ ������ ������ � ��� ���� ������ ���� ����� �����

�������� �� �� ���� %���� ������� ����� ��� ���� ����� ����� ����

��linked list������� � ����������� ����������� �������������������ArrayList����� � ���� � ��� ����� � ����� ������ ���� ������� � ���� ������ �� �

������� ��sets�� ������� ��� ��� �� ����� ����� ��� ������ �������� �����

� � �����stacks�������� ���trees���������hash tables�����������������queues�

������� ����� ����� ������������� ��� �� �� �� �� � ���� ������� ���� � ���

��������� ���������������������� ���������� ��������������� �������

������ ������� ��� �������� ����������� ���� ����������������������� �����

� #�������������������������������������������� �������������� ������

����������� ������������������������ ����iterator��������� �� �����

���� ��� ��� ����� � � � ������ ��������� ����� � ��� ���� ��� �� ���

�� ������� ��� � ���� ��� � ����� ���� ��� ������� ���� � ��� � � ���

��������������������� �� ���� ������������������ ��������� ���

� ������������������� ���������������� � �� ��������������� ���� �����

������������������ ���� ����������� � ����� ��� �� ������� ������

������ ���� ������ ����� ������� ���� ���� ���� ���� ���� � ��� � ��

�������������������, ��Enumeration��������������������$�$��$�1��������

������������ ���� �� �� �� ���������������� ��� �"�������� ��� ���

� ����� �Enumeration� ����������������������� ���Iterator

����� ������������� ����� �������������� ��������� ������� �����

���� ��� ���������������������� � � ����������������������� ��� ��������

��������� �������� � ���� ��������� ���������� ���������� ����� ���� ��

�������� �� � � ������� ��� ���� ����� ������ ����� ������� �����

������ � ����� ������ ����������������� � ���������������

������ ���� ����� ���� ����� ����� ����� ���� ���� ���� � ����� ��� �� ���

� ��� �����������������������������������������������������

���LinkedList�� ����� � ��� � �ArrayList�� ��� � � ���� �� � ����� ��

����� � ������ �� � � ����� ����� �� �� ������ ����� ����� � ���

�������� ���� ����� ��� ����� � ����� �� �� ����� ������ ������ ��

Page 47: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� ��

���� ��������������� �������� ����������� ������ ��������������������

���� ���� � ������� ������ ��� � ����� ������ ����� ����� ���� ��� ��

����������������� ������ ���� ����� ��������������������������� ����

������� ����� ��� �� ��� ������ ���� ���� � ���� � !������������ �����

��������� ������������������������ �������� ������ �� ��� � � ���

����� � ����� � ��� � ��� ���� � ���� ��� �� �� ������� ������� ����

������������������������������������������ ������������ ������ ��

������ ���� ��� ���� � ��� �� �� � ������ ������� ������ ����� �������

� ��������������� ����������� �������� ������ �����������������������

���� �������������� ��� ��� ��������������� � ������� � �����������

��� ��������������������� ����������� ��� ������������������ �� ���

����������� ��� ������������ ���� ������ ���������� � ������ ���

� ���� � ���� ������� �������� ���� �� ������ ������������������������

�!������� ��������������������� ����������������������� � ����������

������ ������� ����� ���������������������� �������� ��������

� � ������ ��������� �������

� #����� �� ��������������� ��� ��� � ��� ������ ����� �������� � ��� ����� ���� � ���

�������������������� ����� �� ������������ ������� ��� ��� � �C++��� � ��� � �������������� � �� ���� � ��� � ���� ���� ������� � ����

���������������������Object������ � ��������������������� ��������

� ����� ���� �������� �����������

�������� ���������� �!���������������������������� ���������� ����

�������������������� ���C++����������� ���� ��� �����������������

������������������ ������������������������������������������

���������� ���������� ������������ �� ����� ���� �C����������� ���

������������������ ����� ���������� � ��� ��� ������������������� ��

�������� ���� � �� ������ ������������� ����� ����� �� �� �� � ��

����������� � ������ ������������������� ����� ������� ���� ����

��������������� ����� ��������� ���� � ���� �� ���� ����� "���������

�������������������� � ������# ���� ��C++������� �������� ���

��� ���������������������� ��������������������C������������������ �

� ���������� �� ������������������������

� � �� ����������������������������������� ���������� ���������� �����

�������� ��� ��� � ���������������� ����� ��������� ��� �� ����������

�������� �������������� ���������������������������������� ������������

� ���C++�������������� ����� ������������� ����� �������

Page 48: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � �

������ �� �� ��� � !���� ����� ��� ���� ���� ���� � ��� � �������

����� ��� � !���� ����� � ������� �� � ���� � ��� ���� �� ���� ��������

�������� � ���� �������� ������� ���� ������ � ��� � ��� ����� ���� ����

� � ����!��������� ������������������������� �����������������

����������������������� ����������� ������ ������������������������

������� ���� ��� � ���� ���� � ��� ����� ���� ����� ������� ��� ����� ��

������� ���� ��exception handling�� �������� ������ ���� ������ � ����� �����

� ���������� ��������

� ���������� �� ������������������������������ ����� � ������ ���������������������������� � ���������

���� ��!�� ���� ��������������������� �� ������������ ���������

� ���������� ����������� ��� �� �� ���������������������������� ��

� �������������$�������

�������� ����������������� ��� � � �� �������� ����� ������������

�������Object�� ������������� ���� �������� ���Object�%���������� �������

� ���� � ��� ����� ��� ��� �� ���� Object� ������ ��������� �� �� � ���

� �������� � ����� �������

�����������������Object������ � ����!�������� ���� �� ��������� �����

����� ����!���� ������Object������������ ����� ��� ����������� � ������

��������� � ���������������Object��� ������������ �� ���������������

������ ������� � ������� �������� �� ���� �Object��� �� ������ � � ��� ��

� # �������� �������� ��������� ����� ������� ���� � ��� ����� �

���������� �� �������� � ���������������� ������� �� ��������������

�� ��� � ����� �� �� ������������������ ����� ���� ���������� �����

���������� ���������� ������������������ ���� ����� �� ����downcasting�

���������� ������������������� ��� �Object�������������������� �������� �

� ����������������� ����������������������� ������� ����������

������������������ ������ ������������������������������ �������� ���

�!��� ����� ��� � ����� ���� �� ������ ��exception������� �� � ���� ��

����� ������ � �� �� �� � ���� ���� ��� �� �� ����� � ����� ���������� ���

� � ���� ����� � �� �������������

�����!��"�������� ���� ��!���� ������ ������ �������� ���� ��

������������ ����������������� ��� �������������� � ������ ������

�#���� ������� ���� � ��� ���� �� �� ��� � ��� ����� ���� � �� ��� ��

������� � �� ��parameterized types�� ���� ��� ����� � ��������� ���� ����

Page 49: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� � � � � � � � � � � � � � � � � �� �!

���� ������ ����������������������� ���������������� �������� � �

������������������� ��� �������� �������� � ���������������� ���

� ������������� ��

�C++������������ ������C++����������������� ���������������������

������� ������� ����� � ���� � ����� �C++� ����� ������ ���� ������� � ���

������� ������� ������ ���� ������� �� ����� ������ template� �� � �������

���� �� ����� ������ ���� ��� ��� �� ���� �� ������ � ���� ���� ��������

����������������������� �� ���������� ��������� ���� �������� ������

� �C++���� ���� ����������� �� ����������������

� )�����(��&���������� ����$�

�����"������������������� �������������� ���������������� ����������

������������������� �������������� ��������������������������

���� ���� ��� %������ ������ ����� ��� ��������� ��� ����� ���

���� ���� ����� ��� ����� ����� �� ���� ��� � �� � ����� �� ������ �������

� ������������� �� ������������� �� ���������

������ ����������������� ������ �������������� �������� ����

����� �������� ���� �� ������ ����������� � �� ������� ����� ���� ���

� ���� ��� %���� ��� � ������ ������� ����� ����� ���� �� ����� ����

������������������������������������������������� ��������� � �

������ ���� ������������������� ������ � ��� ����� ������� ���

� ����� ����� ����� ��� ��

����� ��� ���� %����� � ��� ���� ������ ���� ������ ��� ���� ������ ���

������ � ���� ������ � ��� � ������� ��� ������ ��� ����� ������ ����

������� ������� � ��� ���������� ������� ������ ������� ��� �����

�!���� ������������������� ��������������������������� ���� ���

� ����������� ���������������������������� ���� � ���� ����������

� ������ ������� ������� � ������������� �������������

�#�������� � ��� ����� ����� ���� ����� ����� ���� ����� %���� ��� ��� � ���

� ���� ������� ����� ��� ���� ����� � ��� ���� ���� ������� � ��� ����� �������

������ � �������������� ������������� ��� ��� ������ ���� �� ���� ��

����� ��������� � ��C++������������������������������������������

� ������ ����

���� � ������� ���� ��������� ����������� ���� ��� �!���� �����������

��������������������� ��� �!������������������������������� �������

������������������������������ ����������� � ����������������

��Object� ���� ���� ����������� ���������� ����� ��� ��������� � ��

����� ��� ��� � � ��� ��� � �� ������ �� ���� ����� ��� ��������� ����� ����

Page 50: Authorized translation from the English language edition of · Authorized translation from the English language edition of: Thinking in ... from the 2nd edition of the book "Thinking

� J a v a � � � � � � � � ��

����� � ��� �� �� ���� ����� ��� ��� �C++� ��� ��� �� �� �� ����� ������

� ��������������� �

� �� �%�����$����$��� ������

�����������������#C++������� ���� ���������������������� ����� ���

��� ������� ������ �������� ����� ������� ��� �����������������

�� � � �������stack������� �������������������� ���C++��������������

� ����� � �� ��� ����� ����� � ���� ����� ���� ������ ����� ����

������������� ������� ���� ���� ��� ������������������������� ������ �

��� �� �����heap�� ��������������������� �������������������������

������ � ��� ���� � � ���� ����� ������� ����� � ��� "��� � ��� � �� �� ���

�������� ����� � ��� �!����������������������������������������

�������������������������� � ��������� ���������������������������

� ������ �������������������������������� ���� ������������������

������ ������� �� � ���� � ��� ���� � ��� ���� ����� ��� �������� ������

������������real time��������� �������������������������������!���

� ������������������� ����� ��������

������������������� ���� ��� ���C������������� ����������C++���������

������������C++�������� ���������� � ������������������� ����!�� �

���� ������ ��� � �� � � ���� �C� ����� ����� ����� ������ � � ������

��� � ��� �C++� ��� ���� ���� ������ �C++� ��� ��� � ���� ���� ��� �����

���������� ���� �������� ��������� ������������������������� ����

� � ���� � ���� ��� ����������� �����