Some java test questions and self-made simulation server and client, java test questions

Source: Internet
Author: User

Some java test questions and self-made simulation server and client, java test questions
Media 1, java environment variable: PATH :.; % JAVA_HOME % \ bin; % JAVA_HOME % \ jre \ bin; CLASSPATH :.; % JAVA_HOME % \ jre \ lib \ rt. jar; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar; JAVA_HOME: jdk installation path java running principle: Java application program => Java API => Java Virtual Machine => operating system => underlying hardware java source file compiled into class file to run 2, the sleep method differs from the wait method: the sleep method is a Thread running state control problem. It comes from the Thread class and does not release the lock. It can be used anywhere, but exceptions must be caught. The wait method is a communication problem between threads. It comes from the Object class and releases the lock. It can only be used in synchronous control code blocks in synchronous control mode without throwing an exception. 3. Difference Between StringBuffer and StringBuilder: StringBuffer: a variable class. No change to the string it refers to will produce new objects. Thread security, suitable for multi-threaded use, low efficiency. StringBuilder: The thread is unsafe and suitable for single-thread use with high efficiency. 4. static internal class, which cannot be accessed by external class instance members. It can only access static members of internal classes. The local internal class can only access the local variables modified by final in the method. An anonymous internal class is suitable for only one class. It cannot be an abstract class or a constructor. It must inherit one parent class or implement one interface, but only one parent class or interface can be implemented at most. 5. Current Time: SimpleDate Fomat s = new SimpleDate Fomat ("yyyy-m-d HH: mm: ss"); System. out. println (s. format (new Date (); 6, lazy mode: class Singleton {privorte static singleto a = null; publilc static Singleto getA () {if (a = null) {a = new Singleton ();} return a;} private Singleton () {}} 7. Traverse hashmap: First: Map map = new HashMap (); iterator iter = map. entrySet (). iterator (); while (iter. hasNext () {map. entry entry = (map. entry) iter. next (); Object key = entry. get key (); Object val = entry. getValue () ;}type 2: Map map = new HashMap (); Iterator iter = map. entrySet (). iterator (); while (iter. hasNext () {map. entry entry = (map. entry) iter. next (); Object key = iter. next (); Object val = map. get (key) ;}8, java's basic data type and packaging class: basic data type packaging Class byte Byteshort character int Integerlong Longchar Characterfloat Floatdouble Doubleboolean Boolean 9, convert character type to integer: string str = "17"; int I = Integer. parseInt (str); 10, thread life cycle: Start = "ready =" blocking = "run =" death ==== return === 10, simple factory mode: interface Gongchang {void send Msg ();} class A {public static Gongchang getInstance (String type) {Gongchang g = null; if ("htc ". equalsgnose. case (type) {ff = new HTC ();} else if ("iphone ". equalsgnose. case (type) {ff = new Iphone ();} return p ;}} 11, view the content of a certain text block: File f = new File (document path ); fileReader r = new FileReader (f); char [] c = new char (1024); r. read (); below is my own simulated server and client:

Import java. io. IOException;
Import java. io. InputStream;
Import java.net .*;
Import java. util. collections;

Public class Domefuwu {// server
Static pipeline SC = new pipeline (System. in );
Static Socket socket = null;
Static ServerSocket sever = null;
Public static void main (String [] args ){
System. out. println ("created server ");
Try {
Sever = new ServerSocket (7878); // create a port named sever: 7878
While (true ){
Socket = sever. accept (); // monitoring port sever
System. out. println ("has been connected to" + socket. getLocalAddress () + "chat channel \ n waiting for the other party to speak ");
Duqu (); // call the read Method
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

Private static void duqu () throws IOException {// define the read Method
InputStream input = socket. getInputStream ();
Byte [] bytes = new byte [1, 1024];
Int len = 0;
StringBuffer buffer = new StringBuffer ();
While (len = input. read (bytes ))! =-1 ){
Buffer. append (new String (bytes, 0, len ));
}
Input. close ();
System. out. println ("message sent by the other party" + buffer. toString (); // output the read message
}
}

 

Package duankou;
Import java. io .*;
Import java.net .*;
Import java. util .*;

Public class Domekehu {// Client
Static pipeline SC = new pipeline (System. in );
Static Socket socket = null;
Public static void main (String [] args) throws Exception {
System. out. println ("the client is trying to connect ");
Try {
Socket = new Socket ("127.0.0.1", 7878); // link port: 7878
Try {
Fayan (); // call the speech Method
} Catch (Exception e ){
E. printStackTrace ();
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
Private static void fayan () throws Exception {// define the speaking Method
System. out. println ("the link is successful. Please speak ");
String nei = SC. next (); // input and transmit it to the server
OutputStream output = socket. getOutputStream ();
Output. write (nei. getBytes ());
Output. close ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.