     11 

     java.util n iava.lang 

              Java,  
,    java.util  Java.long,     
. ,        , 
  ,        
  -   Java,  
         
" ,   . 
     ,          
  ,     .  ,  
 ,      ,    , 
,   ,       
  . ,  ,     , 
     . 
          ,    java.util  
Java.long. ,   Runtime  Java.long   
 .     ,     
  - Java,   Runtime   
.         
    Java     .    
          
    Java,       
 Java.long  java.util      . 
      java.util   ,   "" 
,       .   
 :    ;    
(Vector);   ,  - (Hashtable);  
Properties,   -"7    
  "/",  ""/57     
     .      
        .     
   C++  ,   
,  ,      Java  
 Jayg-Kfigccjbi.     MFC CStack, 
CMapXToY   MFC-.   

    321 

    jnaiceT java.util     ,      
 ,     -. 
      java.util    .  
 Date,    java.util.Date,   
     (   )   XX    
 .    ,   - 
    C/C++,   java.util.Random.  
Random       
 ,      rnd()  
random()  . 
    ,  StreamTokenizer,     9,   
  .         
,     ,    ,  
  ,      java.util,  
    java.io. 
       java.lang  ,      
 .     ,   
 . ,    Boolean   
.        , 
  Java     ?     
,    ,   . 
   Vector,       
(  Object),     , 
  ,    .    
   java.lang   Boolean. ,   
          
,  ,        . 
         java.lang    
.    Java-       
 .  Class   Java-   
 Java.  ClassLoader  java.lang   
     Java.     
  Class  - .    
     ,   
    Java.       
  . 
       Math,     
,     ,  
  .  ,  , 
            
  . 
         java.lang    
    .  Runtime  System   
      . ,  

    322	Visual J++:   

               
      Java.  SecurityManager 
    ,    
 .       
 ,       ,  
        .. 
    ,      java.lang   
,    Java.  Object,  
      Java,    
java.lang,    String  StringBuffer.      
 1.  Thread  ThreadGroup,    Runnable  
        Java  
  .     
Exception,       Exception,     java.lang. 

       java.citil 

     ,       
 Java,    .    
  ,  , -   
  ,    java.lang. ,  
       
      java.lang   
  .  -    java.lang  90% 
 ,      . 
    ,      ,  
     Java.     .  
    ,       
 ,      .   
  ,          
 ,    .     
,     ,   . 

     Enumeration 

          -   
   .   
Enumeration      .  
Enumeration      ,    
: hasMoreElements()  nextElementQ. 

     11.  lava.util  iava.lan*	323 

       ,      
 . ,   
,   ,    9.   
,     :    
Enumeration.hasMoreElements() ,    ,  
       nextElementQ.  
    while  : 

    Enumeration enum; 
    //  enum      ... ' 
while(enum.hasMoreElements()) { 
    Object  = enum.nextElement(); 
    // ,    . } 

        Enumeration      
  .   -      
  ,     : 

    Enumeration enum; 
    //  enum     ... for(int index=0; 
enum.hasMoreElements(); index++) { 
    Object  = enum.nextElement(); 
    // ,    . } 

        java.util     
" "  . ,   Vector, 
  ,   elementsQ,   
 ,        Vector,  
   : 

    Vector  vect  =  new Vector(); 
    //     Vector     . 
    //          Enumeration     
    //   Vector. 
    Enumeration  enun  = vect.elements(}; 
    //    ,      Vector. 
While(enum.hasMoreElements())    { 


    324	Visual J++:   


    Object  = enum.nextElement(); 
    //    ... 


     Stack 

       :  ,  .  
      pushQ.     
    .         
,   ().      
  . 11.1. 

        () 
       N  
      
       N+1       
        
       N  
       N-1 ,      

    . 11.1.  

         ,      
     .  peekQ    , 
    ,     ,    
  ()  peekQ   ~  . 

     11.  iava.util n Java.lan* 325 

        Stack  : 

    public Stack(); 

      ,       
java.lang,     Calculator,     
    .     
    ,   
   ,        
(   ). 

     Vector 

      Vector    .    
 ,       ,  
 .  ,   Vector  ,    
      ,     
,       .   , 
        ,  Vector 
   . ,   
  ;     Vector  
      .  
 ,   ,  -   
,  ,     
,    int. 
    *~"  Stack    Vector. ,   
push(), pop()  peek(),    Stack,    
,   public   Vector.  Vector  
   ,       
Vector.        push()  
()         . 

        

     ,    Vector,   . 
       setElementAtQ  
insertElementAtQ  Vector.      :  
 ,   ,  ,    .  
setElementAtQ  ,    ,  . 
 insertElementAt()    ,    
,   ,      .  . 
11.2     ,   
setElementAtQ  insertElementAt(). 

    326	Visual J++:   


    setElementAT 
    / 
    / N   N+l  N+2 
    & 
    vfr 
            N  
    insertElementAT 
    t, 
    / 
    N   N+l  N+2 
        N  

    . 11.2. ,   set ElementAt()  insert ElementAt() 

     addElementQ      Vector.  
    ,   , 
  sizeQ.  setSizeQ   ,  
     .  setSizeQ 
  ,     . 
    ,          
Vector:  addElementQ,     Vector,  
insertElementAtO,     ,   
setElementAllQ,       . 
             Vector:  
removeElementO      Vector,  
remo-veElementAt()     ,   
removeAllEle-ments()   .   removeAllElements() 
    ,     setSizeQ  , 
 . 

     11.  lava.util  iava.lang 327 


      Vector 

     Vector     .    
  elementDatafj  Vector,   
protected.    Vector    ,  
 (capacity),  .  ,    
   ,    ,   
     .   Vector   
,    : 

    public Vector(); 
    public Vector(int initial Capacity); 
    public Vector(int initial Capacity, int capacitylncrement); 

             
   ,   .   
         . 
     capacity()  Vector    ,   
 ,   Vector       
  .  ensureCapacityQ   
    Vector. 
     trimToSizeO  Vector    elementDatafj 
(.. ,   ).       
,    size(). 

        Vector 

     containsQ   , ,  
    Vector.     ,  
  true,       Vector,   
false,   . 
         Vector     
  .        
 ,     .  - ,  
       Vector. 

    import Java.util.Vector; import java.io.*; 
    public class SimpleStringDB { 
    public static void main(String[] astrArgs) { Vector vect = new Vector(); 
    //    ,  //   
, if(I > astrArgs.length) { 
    System.err.printIn("Usage Error:\n 
    SimpleStringDB <filename>"); 
    System.exit(-1) ; ) 


    328	Visual J++:   


    //        Vector. DatalnputStream in; 
try { 
    in = new DatalnputStream( 
    new FilelnputStream(astrArgs[0])); 
    for(String str; (str=in.readLine()).length() > 0;) vect.addElement(str); 
    in.close(); 
    } catch(EOFException eof) { } catch(lOException ioe) { 
    System.err.printIn("Error:\n 
    error rearing from input file "); 
    System.err.println(astrArgs[0] + ioe); 
    System.exit(-1); } 
    //    ,    //  
,   , try { 
    String user_in;. 
    DatalnputStream in2 = new DatalnputStream(System.in); 
    while((user_in = in2.readLine()).length() > 0) { int index; 
    if("QUIT".equals(user_in)) System.exit(-0); 
    if(-l != (index = vect.indexOf(user_in))) 
    System.out.println("\tlndex is : " + index); else 
    System.out.println("\t\" " + user_in + 
    "\" is not in the list"); } 
    } catch(EOFException eof) { } catch(lOException ioe) { 
    System.err.println("Error:\n Reading from user: " +ioe) ; System.exit(-1); 
f  -..I.' ..>:.. . 
    }        ,-...:	.                           . ^  : 
    }	_-.

     indexOf()    ,    
   Vector.       1.  
   

    if(-l != (index = vect.indexOf(str) )) 
         : 
    if(vect.contains(str) ) { 
    int index = vect.indexOf(str) ; 

         indexOf()   , 
 ,     .  , 

     11.  lava.util  iavaJantf	329 

        ,      , 
   Vector.  ,     
        Vector   . 

    public int indexOf(int[]) an, Vector vect, Object o) { int count = 0; 
    for (int nlndex=0; -1 != an[count] = vect.indexOf(o, nlndex); 
    nlndex=an[count++]); 
    return count; } 

    ,  indexOfQ      ,   
     Vector.  last!ndexOf()   
   .     
lastIndexOf()   : ,   ,  
 ,     .    
indexOfQ    ,     ,  , 
   an,         : 

    public int indexesOf(int an[], Vector vect, Object o) { int count = 0; 
    for(int nlndex=vect.size(); -l!=an[count] = vect.lastindexOf(o, nlndex); 
nlndex=an[count++]) ; 
    return count; } 


       Vector 

     Vector   Clonable.  ,    
  clone().     Vector,  
    Vector,     clone(): 

    Vector vectCopy = vect.clone(); 

     copylntoQ  ,    Vector,  
 .       ele-mentData[] 
 Vector,    ,    sizeQ. 
 ,  ,   ,  
,      Vector.     
  ArraylndexOutOffioundsExcep-tion.    : 

    Object!]   aobj   =  new Object[vect.size()]; vect.copylnto(aobj); 


    330	Visual J++:   


     Hashtable 

    -     /.    
,   -.    Vector  
    ,   -  
      -. 
     , Hashtable, ,      
-.      ,   
  .   Object   hashcodeQ.   
   Java.      
    -,     
 -. (,    -  Java 
   ,   hashCodeQ.   
  Java       Hashtable.) 
      -  ,    
     hashCodeQ.    
   32-     
 .        
hashCodeQ,       -. 

        - 

     put()  Hashtable      
-.   -  -.  get() 
 -,       -. 
     remove()  Hashtable     
-.    -,    
-.  clearQ   -   /.  
 Hashtable  ,     .  
    . 
     containsQ  Hashtable     containsQ  
Vector.    true,  ,      
 ,  -,   
-. ,  containsKeyQ   true,  
    - -. 
     sizeQ  Hashtable    ,     
Vector    .     /,   -. 
     elementsQ  Hashtable    Enumeration, 
  -,    -   
.           ,   
   .  -   
  ,     -     elementsQ. 

     11.  iava.util  lava.lan*	331 

     key()  Hashtable   elementsQ.   
-,    -,   
-    .     
-   -,   -.   
 -      -,   
-     -. (  
-,    -   -.) 

    public int getKeysForValue(Object aobj[], Hashtable h, Object val) { 
Enumeration enum = h.keysO; int count = 0; 
    while(enum.hasMoreElements()) { 
    Object objKey = enum.nextElement(); 
    if(val == h.get(objKey)) 
    aobj[count++] = objKey; } 
    return count; 


     Properties 

    -     /.  
-  ,   java.util  ,   
 Hashtable,  ,       
/ jHna_Strmg (   -,  -  
).     Properties. 
    ,  System,      
        Java, 
    Java      Properties.  
  getProperties()  System   Properties, 
    /.      
       System.outPrintStream. 

    import java.util.Properies; 
    public class ShowSystemProps { 
    public static void main(String astrArgsf]) { 
    //    . 
    Properties props = System.getProperties(); props.list(System.out); 


    332 Visual J++:   


         ShowSystemProps: 
    --listing properties -
    java.home=c:\java\bin\.. 
    awt.toolkit=sun.awt.Win32.MToolkit 
    j ava.version=l.0.2 
    file.separator=\ 
    line.separator= 
    Java.vendor=Sun Microsystems Inc. 
    user.name=Wizard 
    os.arch=x86 
    os.name=Windows NT 
    j ava.vendor.url=http://www.sun.com/ 
    user .dir=C: \doc\bra in\ jakarta\chap!2\projects\... 
    Java .class .path=. ;C: \java\lib\classes . zip;c: \java\cla... 
    Java.class.version=45.3 
    os.version=3.50 
    path.separator=; 
    user.home=C:\users\default 

       System  Properties   .   
   ,    Properties  ,   
 Hashtable,    ,  listQ. 
 list()      /  ,   
     IqadQ.    loadQ  
, "   Properties  /. 
       Hashtable,  Properties   Clonable. 
,   cloneQ,      Properties. 
    ,     Hashtable,   Properties  
  .     Properties   
      Properties   
   .     Properties 
   Properties,        
,    ,     
  Properties,    .  
,  Properties  ,     : 
    1.        Properties,   
    . 
    2.       ,    , 
  Properties,         
.      ,  null. 
    3.      Properties,  ,    
   ,       
 Properties,     . 

     11.  Java.util  java.lang	333 

     3  ,      
   Properties,   . 
     getPropertyO  Properties    ,    
get()  Hashtable.       , 
  getPropertyO   -,   get() 
 Hashtable  -.     
getPropertyO,   :   ,  
,    -   Properties   
 . 

     BitSet 

       ,    java.util,  
 BitSet.  BitSet     ,   
    true  false.    BitSet 
   32-  .     
 .        BitSet   , 
  . 
      BitSet       . 
    , , 10 ,   BitSet  
 .    ,    ,  
   false,      
  . 
     BitSet,   Vector,    
.     ,  BitSet 
    .   -  
  ,   BitSet      
 false. 
     get(), set()  clear()       
   ,   .  get()   
   ( false     BitSet  
       set(),   
 ).  set()      true,  
 clear()     false. 
      BitSet   ,    
 BitSet.         
clear()  set().  and()        
BitSet.        bs2 
 bsl  BitSet,     bsl. 

    BitSet bsl = new BitSet(); BitSet bs2 = new BitSet (); 
    //   ... 
    //     .   //   
bsl. bsl.and(bs2); 

    334 Visual J++:   

       ()  ()    
    .       BitSet 
 ,    ^    : 

    Bsl.xor(bsl); 

           java.util,  BitSet 
  Clonable.     ()  BitSet 
     BitSet. 

     - 

      java.util    ,  
  Event  java.awt.  Observable   
   .  Observer   . 
    ,  ,   Observer   ,  
-        .  
-     , 
            
       .   
    ,      
  .      ,  
   -. 

     Observable 

        Java   "-able", 
,       
 .  Vector, Stack  Hashtable   
Clonable,   ,      
   cloneQ. ,   
     ,   Runnable. 
      Observer/Observable  .   
-        
  Observable.    ,   
   java.util,    ,  
   Observable ,      
  .    ,  Observer 
    , 

     11.  iava.util n Java.lan*	335 

         ,   Observable. 
     ImageObserver     : ,  
       ,   
  ImageObserver.  Component  java.awt, 
       Component/Container, 
  ImageObserver.  ,    
       . 
            ?  
    ,     
      Observer    
Observable. ,     Observer    
,   -, ..   Observable. 
  -, ,     
   ,   "Notifier" (")  
"Notifiable" ("")  "Observer" ("")  "Observable" 
("").   -    
,    - . 
    "  Observable   ,   
   ,       
.       Observable.  ,  
  Observer    ,  
  .   Component,   
  ImageObserver,      
  .      
    . (    
   Observer,      
 .)  . 11.3 ,   Observable  
   Observer. 

    . 11.3.  - 

    336 Visual J++:   


      Observer    Observable      

        ,     
,   ,   addObserverQ  
Observable.      ,  ,  
    .      
  deleteObserverQ  Observable.     
   ,     . 
          Observable.     
       ,   
      .     
  addObserverQ     ,    
  . 
     deleteObserversQ     . 
      Observer     . 

      Observable 

         Observable   
  .   , ,    
Observable.    ,       
,     true . 
     setChangedQ  Observable      
true.  hasChanged()      ,  
,    Observable .  clearChangedQ 
    ,       
 hasChanged()   false. 
     ,      , 
  ,     .    
   hasChangedQ  Observable.    
      (  
   ),      
hasChangedQ ,     true    
 . 

      Observer 

       Observable      
 ,      notifyObservers().  
      update()  Observer. 
        notifyObserversQ,   
    .     
 updateQ  Observer.      
notifyObserversQ      Observer, 
    update()  ,    
-  . 

     11.  iava.util n iava.lang	337 

         Observablelnteger.  , 
   setlntQ       
setChangeQ, notifyObservers()  clearChanged().    
         
Observable.    notifyObservers()    
setChanged(),      Observable.   
  notifyObserversQ   ,    
        false. 

    import java.util.Observable; 
    class Observablelnteger extends Observable ( 
    // Private-    . private int m_nVal 
= -1; 
    public Observablelnteger() { } 
    public ObservableInteger(int n) { 
    m_nVal = n; } 
    public synchronized void setVal(int n) { m_nval = n; 
    setChanged(); notifyObservers(); clearChangedO ; } 
    public int getVal() { 
    return m_nval; } 


     Observer 

     ,   Observer,    
     Observable.       
 ,    ,  
  Observer  addObserver()  Observable.   
    notiryObserverQ  Observable (  
   )      
  . 
      Observer    .   updateQ: 

    public abstracts void update(Observable o,   Object arg) ; 
    -           
updateQ  Observable.      ,  

    338 Visual J++:   

     .  arg      ,  
    notifyObserverQ  Observable  
 .  ,       (  
 arg)    notifyObserver()   updateQ 
 Observable. 

      

         /,    
"-".    -, 
  ,      Internet  .  
      -, 
   .    Observable   10 
,  -   . (   
   String      UTF 
DatalnputStream/DataOutputStream).     
,   notifyOb-serverQ  Observable    
  Observable.    Observer.   
  Observable   ,  10 , 
    Observable.     
 5 . 
       -.    
  -       
,   .  -   
 -     ". 

    import java.net.*; 
    import java.io.*; 
    import Java.util.Observable; 
    import Java.util.Observer; 
    public class OserlnputServer extends Observable implements Runnable { //  
    ServerSocket  //   ,  
  , private ServerSocket m_sock; private int m_nPort; 
    //  main()    ,  //  
 ,     // . 
    public static void main (StringU astrArgs){ if(l > astrArgs.length)( 
    System.err.println ("Usage Error:\n UserlnputServer <port>"); 
System.exit(-1); } 


     11.  java.util n Java.lan*	339 


    int   riPort=0; try   { 
    nPort=Integer.parselnt(astrArgs[0]); }   catch   (NumberFormatException  nfe){ 
    System.err.println   ("Usage Error:\n UserlnputServer <port>"); 
    System.exit(-1); } 
    new UserlnputServer(nPort); 
    } 
    public UserlnputServer(int nPort) { 
    //  ,     . 
    m_nPort = nPort; 
    Thread t = new Thread(this); 
    t.start(); 
    //      . 
    try 
    { 
    while (null == m_sock) 
    Thread.currentThread().sleep(SOO); } catch (interruptedException e) { 
    //     . } 
    //      //  
Observer, try { 
    DatalnputStream in = new DatalnputStream(System.in); 
    for(String s; !("quit".equals(s = in.readLine()));) 
    { 
    setChanged(); 
    notifyObservers(new String (s)); 
    clearChangedO ; 
    } 
    m_sock.close() ; } catch (EOFException eof) ( } catch (Exception e) { 
    System.err.println ("Error:\n reading user input."); 
    System.exit(-1); } 
    //  run()   ,   // 
         //  Client, 
public void run() { try { 
    m_sock = new ServerSocket(m_nPort); 
    for(Socket s = m_sock.accept(); true; s = m_sock.accept()) new 
ClientHandler(this, s) ; 


    340 Visual J++:   


    ) catch(lOException ioe) { 
    System.err.println ("Error:\n 
    Opening server or listening:" + ioe); System.exit(-1); } ) } 
    //   Client    . 
    //   Observer     
    //  String,    update().    
    //  ,  . 
    class ClientHandler implements Observer { 
    //      DataOutputStream, // 
   (   ). private DataOutputStream m_out; 
    // ,    Observable  . public 
ClientHandler(Observable , Socket s) ( 
    //   DataOutputStream   Socket, try { 
    m_out = new DataOutputStream(s.getOutputStream ()); } catch(lOException 
ioe) { 
    System.err.println ("Error:\n 
    trying to create new socket stream."); } 
    //   Observer  Observable //   
  , .addObserver(this); ) 
    //  update()   UserlnputServer, //  
    . //      
. public void update(Observable o, Object objText) { try { 
    String strText = (String)objText; m_out.writeUTF(strText); 
    } catch (ClassCastException cce) { //      String. 
} catch (lOException ioe) { 
    System.err.println ("Error:\n 
    wtiting out to a socket: " + ioe); o.deleteObserver(this) ; } i 
    ; Co       .  
   . ,    ,  
 "port" .    Observable 

     11.  lava.util n lava.lang	341 

       10 ,     
.      Observable   
Observer.       notifyObser-vers() 
 Observable,      , 
     Observable. 

    import Java.applet.Applet; import java.awt.*; import java.net.*; import 
java.io.*; import java.util.*; 
    public class UserlnputClient extends Applet implements Observer, Runnable 
{ //     ,  //  , 
 ,   //    
UserlnputClientObservable //     ,    
 // UserlnputClientObservable. private List m_list; private Thread m_threadTimer; 
    private UserlnputClientObservable m_observable; private int m_nPort = -1; 
    //  init()        //  
,     start ()  //  , public 
void init() { 
    setLayout(new BorderLayout()); 
    try { 
    m_nPort = Integer.parselnt(getParameter("port")); } 
catch(NumberFormatException nfe) { 
    add("Center", new LabelC'Need a valid 'port' parameter")); 
    return; } 
    m_list = new LisstO; 
    addC'Center",   m_list) ; 
    add("North", new Label("Last 10 from the server.")); 
    //  start ()   -   //   
UserlnputClientObservable,  Applet  //    
Observer, public void start () { try ( 
    m_observable = new UserlnputClientObservable( InetAddress.getByName(getDocumentBase().getHost()), 
    m_nPort); 
    m_observable.addObserver(this) ; } catch (UnknownHostException uhe) { 


    342 Visual J++:   


    m_observable = null; return; } 
    m_threadTimer = new Thread(this); m__threadTimer.start(); } 
    //  stopO    // UserlnputClientObservable  
-, public void stop() { 
    m_observable.stop(); 
    m_threadTimer = null; } 
    //  run()  -.    //  
 UserlnputClientObservable  //   , 
public void run () { try { 
    while(m_threadTimer == Thread.currentThreadO) { 
Thread.currentThread().sleep(500); if(null != m_observable) 
    m_observable.notifyObservers(); } } catch (InterruptedException ie) { 
    return; } } 
    //  update()   
    // UserlnputClientObservable,     //  . 
Applet   ,   // m_list     
,    // Observer. 
    public void update(Observable o, Object arg) { if(o != m_observable) return; 
    String[] astr = m_observable.getLatestStrings() ; 
    m_list.clear() ; 
    for(int ii=0; ii<astr.length && null != astr[ii); 
    ii++) 
    m_list.addltern(astr[ii] ) ; } } 
    //  UserlnputClientObservable   , 
    //      .   
    //        . 
    //   ,    . 
    class UserlnputClientObservable extends Observable implements Runnable 


     11.  lava.util n iava.lang	343 


    //     ,   10 
    //   (   ) 
    //  ,    ,   
    //  . 
    private String!] m_astr = new String[10]; 
    private Socket m_sock; 
    private Thread m_thread; 
    //    ,   , public 
UserlnputClientObservable(InetAddress addrHost,int nPort) try { 
    m_sock = new Soxket(addrHost, nPort); } catch(lOException ioe) { 
    System.err.println("Error:\n connecting to server"); m_sock = null; 
return; } 
    m_thread = new Thread(this); m_thread.start(); } 
    // Applet    stop(),    //  
 stop(). public void stop() { m_thread = null; if (null != m_sock) try { 
    m_sock.close() ; } catch (lOException ioe) {) 
    // Applet   getLatestStrings()   
    // 10  . 
    public String!] getLatestStrings() { 
    clearChanged(); 
    return m_astr; } 
    //  run()   .   // DatalnputStrem  
   ,    // m_astr. public void run () { int 
m_nStr = 0; 
    try   { 
    DatalnputStream in = new DataInputStream(m_sock.getInputStream()), 
while(m_thread == Thread.currentThread())    { 
    String s  =  in.readUTFO ; 
    m_astr[m_nStr]   =  s; 
    m_nStr  =   (m_nstr  +   1)   %  m_astr.length; 
    setChanged(); ) 


    44 Visual J++:   


    m__sock. close () ; } catch (Exception e) { 
    System.err.println("Error:\n reading from server."); 
    } 
    
    
       java.iitil 

      java.util    .     ,  
,  . , ,  ^ , 
      .     
,  StreamTokenizer,    9,   
      . 

     Date 

      Date          
           . 
      Date  , , , ,   .  
    Date     Date 
           
.     : 

    public Date(); 
    public Date(int year, int month, int date); 
    public Date(int year, int month, int date, int hours, int mins); 
    public Date(int year, int month, int date, int hours, int mins,int sees); 
    public Date(long time); 
    public Date(String strTimeDate); 

      (  )    Date, 
     .     
Date,  0   ,   .   
    Date    .   
     .      
long,    ,    1  1900 
. (       System.currentMillis()). 
        UNIX.   
  : 

    Sat,    12     1995   13:30:00  GMT 

      ""         
  GMT (Greenvich Mean Time     ): 

    Sat, 12  1995 13:30:00 GMT+0500 


     11.  iava.uNI n lava.lan*	345 

         ,   ,   
GMTStringQ  ,    ,   
Internet (,   Date    ). 
      Date   -    :  
(getYearQ, setYearQ),  (getMonthQ, setMonthQ), , ,   
.        ,   
   0,     6. 
     getGMTStringQ  getLocaleStringQ     
,    .  before()  afterQ  
  .  before()   true,  
       Date  
  ,  ,    Date.  
after()   true,      
   Date    ,  
,    Date.   equalsQ  
 true      Date   ,   
  . 
    ,  UTCQ      long,  
   ,    1  1900   
,    Date. 

     Random 

          
  Java    java.util  Random.   
        long  
Random      int, long, 
double  float. 
           
  long,   System.currentTimeMillisQ.  
     long,    
,     .   setSeedQ 
 Random,         
   Random.  setSeedQ    long. 
     nextlntQ, nextLongQ, nextFloat()  nestDoubleQ   
  .  nextlnt()  nextLong() 
   .  nextFloat()  nextDouble() 
     0.0  1.0. 

       java.lang 

       Java     
-      .   
        java.util, 

    346	Visual J++:   

          ,      
Java   .      ,  
  BOKgyrjipocjoro . 
         .     
   -       
   ,     : 

    public void storeVal(Vector vect, int n) { int [ ] an = new int[l]; an[0] 
= n; vect.addElement(an); 
    } 

     ,     ,   , 
   Object. 
             
,    java.lang: 

    public void storeVal(Vector vect, int n) { vect.addElement(new Integer(n)); 

           .  
    ,       
   .     Integer  :  
 ,   . 
           java.lang   
,     ,     
.          
 ,    java.lang. 

    	  

    Boolean	  
    Character	  
    Double	      
    Float	    
    Integer	  
    Long	   
         (Float, Double, Integer  Long) 
  Number.      : 
    public double doubleValue(); public float floatValue(); 


     11.  lava.util n iava.lan*	347 


    public int intValueO; public void longValuef); 

        ,    
      . (  Java 1.1   java.lang 
    Short.        
Java 1.0,    Visual J++.     
 ,    Java,  , 
    Visual J++  .) 
            
 ,       , , 
  . ,   Integer   getlntegerQ, 
       Integer.  parselntQ 
        . 
             
toBinaryStringO, toOctalStringQ, toHexStringQ  toStringQ,    
        
  (      ). 
     ,   parseXXXQ  toA%?String() 
1_-      . 
~81)         Integer. 
  toXXXStrmgQ     ,   
   Integer. (     
toStringO,     Integer   .) 
    ,     ^^   
  ,   ^^'  .  
  ,       
    Stack  java.util.Stack.    
 "  "     

    <>  <>  <> 
     
    5   +   2 
       : 
    <>   [<>   [<>  ...] ]   <> 
    ,  
    52   + 


    348 Visual J++:   


        . ,   
    ((5*4)   +   10   +  20   /   (2   +   1) )   %   7 
       : 
    5 
    4 
    *	// (5 * 4) 
    10 
    +	//(5*4)+ 10 
    20 
    2 
    1 
    + 
    /	// 20 / (2 + 1) 
    +	// 5 * 4) + 10 + 20 / (2 + 1) 
    7 
    %	// ((5 * 4) + 10 + 20 / (2 + 1) ) % 7 

    ,      ,    
.          
    "  ".  
        . 
       Double.  
 ,   '+'  '*',     
,  ,      .  
       .    
  Java: 

    import java.util.Stack; import java.io.*; 
    public class RPCalculator { 
    public static void main(String astrArgs[]) { 
    //    . 
    //    System.in  
    //    (),  . 
    //    .    
    //     
    //   . 
    Stack stack = new Stack (); 
    try { 
    String str; 
    DatalnputStream in = new DatalnputStream(System.in); while(true) { 
    str = in.readLine(); 
    //   ,   // -, if("+".equals(str)) 


     11.  iava.util  iava.lan*	349 


    doAdd(stack); else if ("-".equals(str)) 
    doSub (stack); else if ("*".equals(str)) 
    doMult(stack); else if("/".equals(str)) 
    doDiv(stack); else if("%".equals(str)) 
    doMod(stack); 
    else if("quit".equals(str)) 
    break; else try {     // ^  . 
    Double d = Double. valueOf (str) ;"'""" 
    stack.push(d); } catch (NumberFormatException nfe) { 
    System.err.println("\tWhat?"); 
    //    . doShowResult(stack); } } catch 
(EOFException eof) { 
    //  ,    . doShowResult(stack); } 
catch (JOException ioe) { 
    System.err.println("10 error: " + ioe); System.exit (-1); 
    } 
    public static void doAdd(Stack s) { Double opl, op2; try { 
    opl = (Double)s.pop(); 
    op2 = (Double)s.pop(); 
    s.push(new Double(opl.doubleValue() + 
    op2.doubleValue())); ) catch (ClassCastException cce) { 
    System.err.println("Error:\n improper args on the stack.") 
    System.exit(-1); } 
    public static void doSub(Stack s) { Double opl, op2; try { 
    opl = (Double)s.pop (); 
    op2 = (Double)s.pop(); 
    s.push(new Double(op2.doubleValue() - opl.doubleValue())). } catch 
(ClassCastException cce) { 
    System.err.println("Error:\n improper args on the stack."); 


    350 Visual J++:   


    System.exit(-1); } 
    } 
    public static void doMult(Stack s) { Double opl, op2; try { 
    opl = (Double)s.pop(); 
    op2 = (Double)s.pop(); 
    s.push(new Double(opl.doubleValue() *op2.doubleValue())) } catch 
(ClassCastException cce) { 
    System.err.println("Error:\n improper args on the stack.") 
    System.exit(-1) ; } ) 
    public static void doDiv(Stack s) { Double opl, op2; try { 
    opl = (Double)s.pop(); 
    op2 = (Double)s.pop() ; 
    Double d = new Double(op2.doubleValue() / opl.doubleValue()); 
    if(d.isNaN() II d.islnfinite()) { 
    System.err.println("Illegal arithmetic operation."); System.exit(-1); ) 
    s.push(d); ) catch (ClassCastException cce) { 
    System.err.println("Error:\n improper args on the stack."); System.exit 
(-1); } } 
    public static void doMod(Stack s) { Double opl, op2; try { 
    opl = (Double)s.pop(); :    op2 = (Double)s.pop(); 
    Double d = new Double(op2.doubleValue() % opl.doubleValue()); if (d.isNaNO 
II d.islnf inite ()) { 
    System.err.println("Illegal arithmetic operation."); System.exit(-1); } 
    s.push(d); } catch (ClassCastException cce) { 
    System.err.println("Error:\n improper args on the stack."); System.exit(-1); 
    //  doShowResult()      //  
  Stack,   //  Vector. 


     11.  lava.util n java.lan*	351 


    public static void doShowResult(Stack s); 
    System.out.println("\tStack's current state:"); for(int ii=0; ii<s.size(); 
ii + + ) 
    System.out.println("\t" + s.elementAt(ii)); } } 


      Class    

         1,   ..   
-.       ,   
    Java. ,    
Java-  HTML-,      -  
  URL.         
 ,    ,     
  (    CLASSPATH,   , 
  -),   URL. 
              Class. 
   ,    ,  
        ,  
.        Class. 
         Class,  ,    
        .  ,  
,      .   
 ,     ,  
        . 

     Class 

      getClassQ  Object,     
 ,      Class,   
 . ,     Vector   
    Class,   Vector,    
getClassQ  Vector.    : 

    Vector vect = new Vector(); 
    Class classVector = vect.getClass(); 

     getClassQ    Class   . 
         Class    
.   forName()  Class    Class 
  ,      . ,  
      Class   "Object": 

    Class classObject = Class.forName("Object"); 

    352	Visual J++:   

     forNameQ   ,, , '   
  .  forNameQ      
    .     ,  
       .  - 
       ,  
 CLASSPATH,          
       Class.     
   ,    Java. 
           Tjma_Class? ,   
     .  newInstanceQ  Class 
   .       
   new ~  .  
   ,        
.       for-NameQ  Class 
  ,        
   . 
              
 .   ^     
        newInstanceO  
Class.     HelioWorldFrame,    
"Hello, World!"   .    InstantiateClass  
  ,      . 11.4. 

    public class InstantiateClass { 
    public static void main(String[] astrArgs) { 
    //      , //  
  , //    . for(int ii=0; 
ii<astrArgs.length; ii++) { try { 
    Class els = Class.forName(astrArgs[ii]); Object  = els .n?wI_nstance () ; 
} catch (ClassNotFouTTdException cnfe) { 
    System.err.println("Class not found: " + astrArgs[ii]); } catch 
(InstantiationException ie) { 
    System.err.println("Instantiation error with class " + astrArgs[ii]); } 
catch (IllegalAccessException iae) { 
    System.err.println("No rights to constructor for 
    class" + astrArgs[ii]); } catch (NoSuchMethodException nsme) { 
    System.err.println("Class doesn't have no-arg constructor" + 
astrArgs[ii]); } } } 1 


     11.  iava.util  java.lang	353 


    HelloWoildFiame - Miciosol! Developei Studio 
    jj He :?tlit;  :|(. firojecl   Juld lools  ^refew   Help 
    FTT7^|| ; arft.g. | i,  ^ P i 3 
    Ji-*' '* 
    4? a i>3-; "i:! 
     Workspace ;1^9[': 1 pro ffl-S HeiioWoildFrame fiies       "" 
    ^1^:|,|>|, g'jjWjwj 
    public HelloWorldFrame()   { 
    sup<sr( 'Hello,   World!!!"); resize(200,   150): show() 
    } 
    public boolean handleEvent(Event evt) { if(Event.?IHDOtf_DESTROY  evt.id) 
{ dispose(); return true; 
    } 
    return false; } 
    Configuration: HelloWorldFrane - Javt. Compiling... 
    Microsoft (R) Visual J++ Compiler Version 1.01.7022 Copyright (C) 
Microsoft Corp 1996. All rights reserved. 
    HelloWorldFrame. class - 0 error(s). 0 warning(s) 
     

    Puc. 11.4.  HelloWorldFrame,     InstantiateClass 

         InstantiateClass,   
     ,     . 
,     ,     
.     Label   java.awt   
     "java.awt.Label". 

      HelloWorldFrame   : 

    import java.awt.*; 
    public class HelloWorldFrame extends Frame { public HelloWorldFrame() { 
    super("Hello, World!!!"); resize (200, 150); show(); } 
    public boolean handleEvent(Event evt) ( if(Event.WINDOW_DESTROY == evt.id) 
{ dispose (); return true; } 
    return false; } } 


    354 Visual J++:   

    ,       InstantiateClass,   
  catch,    Class.forName()  
Class.newlnstance().        
,           
 .      . 
            -,  
  Class.forNameQ     
ClassNotFoundException.    ,      
. ,      , 
   .  ClassNotFoundException   
    ,     -. ,  
  ,    . 
      InstantiationException    ,  
       . , 
  InstantiateClass      
"java.lang.Observer",         
 .    ,    
"java.lang.Observer"       ( 
Observer   java.lang).     
  .        
 InstantiationException. 
      Illegal Access Exception    ,  
 newInstanceQ   ,      
  public.    newlnstance(),   
        . 
      public,    , 
     .    ' IllegalAccessException. 
    ,   NoSuchMetodException   
newInstanceQ,        .   
,   newInstanceQ     
  .     ,   
    NoSuchMetodException. 
    ,    newlnstance()  forNameQ   
       ,   
         
ClassNotFoundException, InstantiationException, IllegalAccessException  NoSuchMetodException. 

       Class 

     isInterfaceQ  Class   true,    
Class   ,  .      
 false.      ,  

     11.  iava.util  java.lang	355 

       InstantiationException,   ,  
    ,    . 
        ,  getNameQ  Class  
    . 
     getlnterfacesQ     Class,   
    ,  .  
       ,   
.     ,   . 

    public class Listlnterfaces { 
    public static void main(String[] astrArgs) { for(int ii=0; 
ii<astrArgs.length; ii++) { try { 
    Class els = Class.forName(astrArgs[ii]); Class[] aclslnterfaces = 
els.getlnterfaces (); System.out.println("Class " + els.getName() + ":"); 
for(int jj=0; jj<aclslnterfaces.length; jj++) 
    System.out.println("\t" +aclslnteerfaces[jj].getName(}); } catch 
(ClassNotFoundException cnfe) ( 
    System.err.println("Error:\n class not found " + 
    astrArgs[ii]); } j ) } 

     getSuperClassQ      Class,  
   . ,      
 java.awt.Container     getSuperclassQ   
   Class,   Component,   Container 
    Component. 
    ,  getClassLoaderQ  Class     
,       .     
      ClassLoader,     
  ,     ,      
 Class. 

      

    ,      ?   "",  
 -       Internet (  
  )?   ,       
-?      (class loader).  
ClassLoader    ,  
 -,      Java.   
 ,      ,   
URL  - . 

    356	Visual J++:   

          "Foo"    Java  
     "Foo.class"   ( ZIP-),  
  CLASSPATH.     ,      
 ..    defmeClassQ,   private, 
 _ _     .  definejClassQ 
sarpj^a^eTJataccj^cpeflyjbmOTHeH       
  Class. 
          .  
getClassLoaderQ  Class      . 
    . 

    public class GetThisClassLoader { 
    public static void main(String[] astrArgs) { 
    new GetThisClassLoader(); } 
    public  GetThisClassLoader()    { Class   els   =   this.getClass(); 
ClassLoader  cl  =  els.getClassLoader(); System.out.printlnt"The " + 
cls.getNameO   + " class ClassLoader:   " + cl); 
    } i 

             : 

    The GetThisClassLoader class ClassLoader: null 

       null?    ,  
-  Ha^3jrjpjf3j4jjKjgraccoB,   ^ 
.    jprotected,     
    . ,   
    ,    
 ,     . 
        ,       
.  getSystemClassQ  ClassLoader    
   . 
    ,     _    
?   ,  ,    , 
    .    
 ,        
__ ^^^  "   
   . 

        

     ,        
 -  Internet. ,   ,   , 
  .   ,   ,  
 ,   -      
,    URL ,   

     11.  iava.util  java.lang	357 

      -  . ,  -  
  URL "http://www.whatever.com/MyApplet.class",     
     "MyHelperClass",     
  URL "http://www.whatever.com/MyHelper-Class.class". 
             
java.lang.ClassLoader       loadClassQ. 
    - (     Internet)  
  defineClass()  ClassLoader,   protected, 
  ,    URL.  .   
   URLClassLoader: 

    import java.net.*; import java.io.*; 
    class URLClassLoader extends ClassLoader { private URL m_urlBase; public 
URLClassLoader(URL urlBase) { 
    m_urlBase = urlBase; } 
    public Class loadClass(String strClassName, boolean resolve) 
    throws ClassNotFoundException { Class els;  try { 
    URL urlClassFile = new URL(m_urlBase, StrClassName +".class"); InputStream 
is = urlClassFile.openStream(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); int n; 
    while (-1 != (n = is.readO)) out.write(n); 
    els = defineClass(out.toByteArray() , 0, out.size()); } catch (lOException 
ioe) { 
    throw new ClassNotFoundException(strClassName); ) 
    if (resolve) 
    resolveClass(els); 
    return els; 
    } 
    1 

      URLClassLoader,   ,  
 InstantiateClass,      URL.  
,  InstantiateURLClass, ,   ,  
  .     URL,  
    .     
   ,   

    358	Visual J++:   

         .     
InstantiateURLClass,      URLClass Loader: 

    import java.net.*; 
    public class InstantiateURLClass { 
    public static void main(String[] astrArgs) { 
    //     URL,  //   
 URLClassLoader. URLClassLoader url_cl; try { 
    URL urlBase = new URL(astrArgs[0]}; 
    url_cl = new URLClassLoader(urlBase); } catch (MalformedURLException mue) { 
    System.err.println("Usage Error:\n InstantiateURLClass "} ; 
    System.err .println ("<url-base> <classname> [<classname> ...]") ; 
    System.exit(-1); } 
    //        //   
  . for(int ii=l; iiOstrArgs. length; ii + + ) { try { 
    Class els = url_cl.loadClass(astrArgs[ii], false); Object  = 
els.newlnstance(); } catch (ClassNotFoundException cnfe) { 
    System.err.println("Class not found: " + astrArgs[ii]); } catch 
(InstantiationException ie) { 
    System.err.println("Instantiation error with 
    class " + astrArgs[ii]); 
    System.err.println("\texception is: " + ie) ; } catch 
(IllegalAccessException iae) { 
    System.err.println("No rights to constructor for 
    class " + astrArgs[ii]}; } catch (NoSuchMethodException nsme) { 
    System.err.println("Class doesn't have no-arg 
    constructor" + astrArgs[ii]); } } i ) 


     Math 

        ,   
  ,   ,   
,   java.lang.Math.   Math    
,   public,       
   "Math"  : 

    int nMin = Math.min(10, 20); 


     11.  iava.util n java.lang	359 

    ,      Math,   
.        Math   
  double,  PI,     
   .      
  ,    ,     
    : 

    public double degreesToRads(double dDegrees) { 
    return (Math.PI * dDegrees / 180.0); } 
    public double radsToDegrees(double dRads) { return (180.0 * dRads / Math.PI); 
    } 

      Math   random(),     
    ( double).  
     Random  java.util,   
randomQ  Math,        
    ,    
Random (        
     ),     
  random()  Math.     
     . 
       Math :   , 
 ,  ,    . 
   Math   Info Viewer Visual J++.  
        ,     
   .     
    Java. 

        

         Java    
   java.lang.  Runtime  System,   
  ,    
       ,    
 Java-. 
     Java       
   (..     ) .  
SecurityManager     ,  
         , 
   Internet,   , 
      

    360 Visual J++:   

     ..    ,   Java,  
  ._      
,      . 
      ,  Java-    . 
 Process  ,    Java. 
(        . 
          .) 

     Runtime  System 

     Runtime  System  java.lang     
 ,       Java.   
     ,     
 ,       
,         . 
     Runtime        
   .       
    ,  . 

       

         Java-   () 
 Runtime.         : 

    public Process exec (String	command); 
    public Process exec(String	command, String envp[]); 
    public Process exec(String	cmdarray[]); 
    public Process exec(String	cmdarrayt], String envp[]); 

          ,   
.      ,    
    . 
            
,   .     , 
    .   envp[]    
 .     : "  = ". 
            
    .     ,  
 ,       
 ,     . , , 
 ,     (): 

    Process proc = Runtime.exec("Java AnotherClass argl arg2"); 


     11.  java.util  Java.lang 361 

      ,      (): 

    String[] astr_cmndline = {"Java", "AnotherClass", "argl", "arg2" }; 
Process proc = Runtime.exec(astr_cmdline); 

         ,      
 ,   .   
/,    envp[],  ,   
 envp[]    . 

     Process 

       ()  Runtime   Process. 
 Process        
 Java.    Process,   
  Java-   . 
      Process  ,     
-      .  getln-putStreamO 
  . ,    ,  , 
     .  getOutputStream()  
Process   . ,    ,  
     . 
     getErrorStream()   . ,    
,  ,   ,    . 
    ,    Java- (   
 ),  ,    System.outPrintStream  
 ,       , 
  getlnputStreamQ  Process.    
    System.in,    ,   
   ,   Process.getOutputStream(). 
     exitValue()  Process   ,  
  .     Java-,  
   ,   System. exitQ.  
        ,    
 IllegalThreadStateException. 
         Java-    
 ,   Runtime.execQ,   
waitForQ  Process.      
     .     
     ,  waitForQ   . 
     destroyQ  Process    
 . 
        Process  Runtime     
    Java. 

    362 Visual J++:   


    ,      

     freeMemoryQ  totalMemoryQ - Runtime    
   .  freeMemory() ,   
 ,   totalMemory()      
.   ,   Java      
 (    ),    
. ,          
    Runtime.free.Memory()  Memory.Exception. 
               
          
Runtime.gcQ  Runtime.runFinalizationQ.  gc()     
  .  runFinalizationQ    
,   ,    . 

     System 

        System     
,       Java.    
 System      ,  -  
 , ""     .    
Runtime    System (,  exit()  RuntimeQ 
    System).	"""""*""" 

        

           ,  
     Java,    .   
Properties  -,      . 
  ,   Info Viewer,  
    . 
    ,  ,  "user.dir",   
       .  
"user.home"     .  
"user.name"  ,       
.   getPropertiesQ  System   
   Info Viewer.     
,         Properties. 

     -    

              
  System.in, System.out  System.err.    
  ,       Java. 

     11.  java.util n iava.lang 363 

       System.in  System.out  , .. 
       .    
      AWT.  
System.err     ,    
 .   Netscape Navigator   System.err 
   Java Console,   ,    Options 
 Java Console. 
    ^J^ya       System.in  
    System.out.       
  .        ,  
  _    . 

     () 

     System.arraycopy()         
  .  ,       Java, 
   - .     
     . 
    ,         , 
   : 

    public int[] doublelntArraySize(int[] an) { int[] anRet = new 
int[an.length * 2]; 
    System.arraycopy(an, 0, anRet, 0, an.length); 
    } 

      arraycopyQ   : 

    public void arraycopy(Object  src,   int  srcOffset,   Object  dst, 
    int  dstOffset,   int  length); 

     arraycopyQ    Array Store Exception JB 
 ,         
. "   ,    
,     ArrayOutOfBoundsException.    
    ,        
    ,      . 
    ,  currentTimeMillis()  System    
long,    .    
 ,     1  1900 .  
  ,     Date   
    . 

      

     Java       
  .     

    364 Visual J++:   

            
  . 
    ,          
 ,      
  .  -    
    ,     
    ? 
         ,  . 
 ,    Applet     
     .     
     ,   , 
         
 -.      
 . 
      SecurityManager   public-  
checkXXXQ.     : 

    public void checkXXX(<args>) ; 

        ,    
checkXXXQ  .    ,   
81"   Security Exception. 
    ,       ,    
checkReadQ  SecurityManager.       
 SecurityException,       . 
,        
checkConnectQ.          
,    ,     . 
      SecurityException, 
    . 
         Visual J++    
checkA%Y()  SecurityManager.  ,     
  void.      false  true, 
SecurityManager        SecurityException. 

     11.  java.util  iava.lang 365 

