Some java test questions and make your own simulation server and client

media
1, java environment variables:
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
How java works:
Java Application => Java API => Java Virtual Machine => Operating System => Underlying Hardware
Java source files are compiled into class files and run
 
2. The difference between sleep method and wait method:
The sleep method is a problem of thread running state control. It comes from the Thread class and does not release locks. It can be used anywhere, but exceptions must be caught.
The wait method: It is a communication problem between threads and threads. It comes from the Object class and releases the lock. It can only be used in the synchronization control code block of the synchronization control method, and does not need to throw an exception.
 
3. The difference between StringBuffer and StringBuilder:
StringBuffer: is a mutable class, any change to the string it refers to will not generate a new object. Thread safety, suitable for multi-threaded use, low efficiency.
StringBuilder: thread-insecure, suitable for single-threaded use, and high efficiency.
 
4. In a static inner class, instance members of the outer class cannot be accessed, but only static members of the inner class can be accessed.
A local inner class can only access the final variables in the method.
Anonymous inner classes, suitable for classes that are only used once, cannot be abstract classes, cannot define constructors, must inherit a parent class or implement an interface, but can only have at most one parent class or implement an interface.
 
5. Current time:
SimpleDate Fomat s = new SimpleDate Fomat("yyyy-m-d HH:mm:ss");
System.out.println(s.format(new Date()));
 
6, the lazy mode:
class Singleton{
  privorte static singleto a = null;
  publilc static Singleto getA(){
    if(a == null){
      a = new Singleton();
    }
    return a;
  }
  private Singleton(){}
}
 
7. The way to traverse the hashmap:
The 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();
}
The second:
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. Basic data types and their wrapper classes in java:
Basic data type wrapper class
byte Byte
short     Short
int      Integer
long     Long
char     Character
float     Float
double    Double
boolean    Boolean
 
9,将字符型转换成整形:
String str = "17";
int i = Integer.parseInt(str);
 
10,线程的生命周期:
开始==》就绪==》阻塞==》运行==》死亡
        《===返回===
 
10,简单的工厂模式:
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,查看某文挡下的内容:
File f = new File(文档路径);
FileReader r = new FileReader(f);
char [] c = new char(1024);
r.read();
 
下面是我自己做的模拟服务端和客户端:

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Scanner;

public class Domefuwu {                              //服务端
  static Scanner sc = new Scanner(System.in);
  static Socket socket = null;
  static ServerSocket sever = null;
  public static void main(String[] args) {
    System.out.println("已经建立服务器");
    try {
      sever = new ServerSocket(7878);                  //新建一个端口,名为sever:7878
      while(true){
        socket = sever.accept();                     //监视端口sever
        System.out.println("已经和"+socket.getLocalAddress()+"建立聊天通道\n等待对方发言");
        duqu();                              //调用读取方法
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void duqu() throws IOException {               //定义读取方法
    InputStream input = socket.getInputStream();
    byte[] bytes = new byte[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("对方发来消息"+buffer.toString());           //将读取到的消息输出
  }
}

 

package duankou;
import java.io.*;
import java.net.*;
import java.util.*;

public class Domekehu {                           //客户端
  static Scanner sc = new Scanner(System.in);
  static Socket socket = null;
  public static void main(String[] args) throws Exception {
    System.out.println("客户端正在尝试连接");
    try {
      socket = new Socket("127.0.0.1",7878);             //链接端口:7878
      try {
        fayan();                           //调用发言方法
      } catch (Exception e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private static void fayan() throws Exception {             //定义发言方法
    System.out.println("链接成功,请发言");
    String nei = sc.next();                      //输入并传输给服务端
    OutputStream output = socket.getOutputStream();
    output.write(nei.getBytes());
    output.close();
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324905950&siteId=291194637