在 Math 類中,爲了達到最快的性能,全部的方法都使用計算機浮點單元中的例程,若是獲得一個徹底可預測的結果比運行速度更重要的話,那麼就應該使用StrictMath類,它使用 「自由發佈的 Math 庫 」 ( fdlibm ) 實現算法 , 以確保在全部平臺上獲得相同的結果。html
不要在boolean與任何數值類型之間進行強制類型轉換,防止錯誤java
不要用== 運算符測試字符串的相等性:只有字符串常量是共享的, 而 + 或 substring等操做產生的結果並非共享的。算法
不要使用 char 類型,太過底層,UTF-16須要兩個代碼單元,用charAt會有問題。編程
在循環中 , 檢測兩個浮點數是否相等須要格外當心 。json
從 Java SE 7 開始 , case 標籤還能夠是字符串字面量數組
帶標籤的break服務器
int j = 10; breakpoint: for (int i=0;i<10;i++){ while (j-->0){ if(j==5){ break breakpoint; } } } System.out.println(j);
能夠將標籤應用到任何語句中 , 甚至能夠應用到 if 語句或者塊語句中網絡
只能跳出語句塊,而不能跳入語句塊dom
Biglnteger 類實現了任意精度的整數運算 , BigDecimal 實現了任意精度的浮點數運算socket
數組長度爲 0 與 null 不一樣
Arrays.copyOf :一個數組的全部值拷貝到一個新的數組中
運行時常量池從方法區中分配
Map<Integer, List<Integer>> map = random.ints(size, 0, bound).boxed().sorted().collect(Collectors.groupingBy(value->value/10));
XML 指可擴展標記語言
XML 被設計用來傳輸和存儲數據。
JavaScript對象表示法(JavaScript Object Notation),是輕量級的文本數據交換格式
JSON 是存儲和交換文本信息的語法。相似 XML。
JSON 比 XML 更小、更快,更易解析。
public static void readJson(){ try { JsonParser jsonParser = new JsonParser(); JsonObject jsonObject = (JsonObject) jsonParser.parse(new FileReader("test.json")); System.out.println(jsonObject.get("cat").getAsString()); System.out.println(jsonObject.get("pop").getAsBoolean()); JsonArray array = jsonObject.get("book").getAsJsonArray(); for (int i=0;i<array.size();i++) { System.out.println("===="); JsonObject subJson = array.get(i).getAsJsonObject(); System.out.println(subJson.get("id").getAsInt()); System.out.println(subJson.get("name").getAsString()); } } catch (FileNotFoundException e) { e.printStackTrace(); } }
使用http的Get讀取網絡數據
public static void doGet(){ URL url = null; try { url = new URL("http://www.dengyouquan.cn"); URLConnection connection = url.openConnection(); } catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } try(BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));) { String line = null; while ((line=reader.readLine())!=null){ System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }
使用http的Post與網絡通訊
public static void doPost() { URL url = null; HttpURLConnection connection = null; try { url = new URL("http://www.dengyouquan.cn"); connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("encoding", "UTF-8");//添加請求屬性 connection.setDoInput(true);//容許輸入 connection.setDoOutput(true);//容許輸出 connection.setRequestMethod("POST");//POST請求 } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (connection == null) return; //writer與reader順序一致 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); ) { writer.write("?p=6"); writer.flush(); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }
使用HttpClient進行Get通訊
public static void doHttpClientGet(){ HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("http://www.dengyouquan.cn"); try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) httpClient.execute(httpGet);){ HttpEntity entity = httpResponse.getEntity(); if(entity!=null){ String entityStr= EntityUtils.toString(entity,"utf-8"); System.out.println(entityStr); } } catch (IOException e) { e.printStackTrace(); } }
使用HttpClient進行Post通訊
public static void doHttpClientPost(){ try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://www.dengyouquan.cn"); List<BasicNameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("p","6")); httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
Socket是應用層與TCP/IP協議族通訊的中間軟件抽象層,它是一組接口。
HTTP1.0默認是短鏈接,無狀態的.
Http1.1默認是長鏈接
半包:接受方沒有接受到一個完整的包,只接受了部分;
粘包:發送方發送的多個包數據到接收方接收時粘成一個包。
TCP:流,會有半包和粘包
UDP:數據包,無半包和粘包
ServerSocket和Socket
使用Socket時用BufferedReader會阻塞
socket通訊服務器端
Socket編程中,儘可能用PrintWriter取代BufferedWriter
PrintWriter的println方法自動添加換行,BufferedWriter須要顯示調用newLine方法
PrintWriter的print、println方法能夠接受任意類型的參數
PrintWriter和BufferedWriter的使用區別
Scanner的平均耗時是BufferedReader的10倍左右
java Scanner與BufferedReader讀取鍵盤輸入性能比較
推薦Scanner
java控制檯輸入 InputStreamReader與Scanner ,Console 區別
public class ServerSocket1 { private static final int PORT = 8888; public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(PORT)) { System.out.println("服務器已啓動..."); int j = 0; while (j++<Integer.MAX_VALUE){ Socket socket = serverSocket.accept(); new Thread(new ServerSocketHandler(socket)).start(); } } catch (IOException e) { e.printStackTrace(); } } } class ServerSocketHandler implements Runnable { private Socket socket; ServerSocketHandler(Socket socket) { this.socket = socket; } @Override public void run() { try(BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))){ String line = null; System.out.println("服務器已鏈接..."); boolean exit = false; while (!exit){ System.out.println("等待消息"); String msg = reader.readLine(); switch (msg){ case "": continue; case "date": LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime:"+localDateTime); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); writer.write(dateTimeFormatter.format(localDateTime)+System.lineSeparator()); break; case "exit": System.out.println("server close..."); writer.write("exit"+System.lineSeparator()); exit = true; break; default: writer.write("commend not found!"+System.lineSeparator()); } writer.flush(); } } catch (IOException e) { System.out.println("客戶端異常退出。。。"); } } }
socket通訊客戶端
public class ClientSocket implements Runnable { private static final String ADDRESS = "127.0.0.1"; private static final int PORT = 8888; public static void main(String[] args) { new Thread(new ClientSocket()).start(); } @Override public void run() { try(Socket socket= new Socket(ADDRESS,PORT); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); Scanner scanner = new Scanner(System.in);){ boolean exit = false; System.out.println("客戶端已鏈接..."); while (!exit) { writer.write(scanner.nextLine() + System.lineSeparator()); writer.flush(); String line = reader.readLine(); System.out.println("客戶端收到消息:"+line); if(line.equals("exit")){ exit = true; } } } catch (IOException e) { System.out.println("無服務器"); } } }