Java

Java内存模型的8个操作步骤:
lock,unlock,read,load,use,assign(赋值),store(存储),write

重写equals

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(name).append(code).toHashCode();
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof WlCompany))
            return false;
        WlCompany castOther = (WlCompany) other;
        return new EqualsBuilder().append(name, castOther.getName()).append(code, castOther.getCode()).isEquals();
    }

输入流:FileInputStream 输出流:FileOutputStream
字节流:InputStream,OutputStream
字符流:InputStreamReader_BufferedReader,BufferedWriter

FileIntputStream fin = new FileInputStream("d:/a.txt");
FileOutputStream out = new FileOutputStream("d:/b.txt");
byte[] buff = new byte[256];
int len = 0;
while((len = fin.read(buff)) > 0 ){
        out.write(buff,0,len);
}
fin.close();
out.close();

运算符优先级

Java_第1张图片

反射

package
 Reflect;

class
 Demo{ 
//other codes...
}

 
class hello{
public static void main(String[] args) {     
Class demo1=null;       
Class demo2=null;      
Class demo3=null;
        
try
{            
//一般尽量采用这种形式
            
demo1=Class.forName("Reflect.Demo");
        
}
catch
(Exception e){           
e.printStackTrace();       
}
        
demo2=new Demo().getClass();
        
demo3=Demo.class;        
        
System.out.println("类名称   "+demo1.getName());
        
System.out.println("类名称   "+demo2.getName());
        
System.out.println("类名称   "+demo3.getName());       
   
}

}

【运行结果】:

类名称 Reflect.Demo

类名称 Reflect.Demo

类名称 Reflect.Demo

用TCP通信模型创建一个WEB服务器

package test;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class WebServer {
    public static void main(String[] args) throws IOException{
        //用端口号创建一个ServerSocket对象,监听Web的端口80
        ServerSocket ss = new ServerSocket(80);
        //开始循环监听来自客户端的请求
        Socket s = null;
        while ((s = ss.accept()) != null) {
            new HTTPThread(s).start();
        }
        ss.close();
    }

}

class HTTPThread extends Thread{
    private Socket socket;
    public HTTPThread(Socket socket){
        super();
        this.socket = socket;
    }
    
    public void run(){
        try {
            OutputStream os = socket.getOutputStream();//获得输出流
            PrintWriter pw = new PrintWriter(os);
            //往输出流写出当前时间
            pw.println("");           
            pw.println("");           
            pw.println("hello world!");         
            pw.println("");          
            pw.println("");  
            pw.flush();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

使用java访问Web站点

    URL url = new URL("http://www.jianshu.com/users/cb1ba0e78b56/latest_articles");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();
        //获取响应信息的头部信息
        Map> header = conn.getHeaderFields();
        //获取响应内容
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
        br.readLine();
        //...
        conn.disconnect();

你可能感兴趣的:(Java)