A record of some Java fundamentals.
Java Basic series:
Java Basics-1
Java Basics-2
ArrayList and LinkedList implementation of the bottom layer
ArrayList is an implementation class of the list interface, which is characterized by high query efficiency, low deletion efficiency and thread insecurity.
The reason is that the ArrayList layer encapsulates an array, which is implemented using an array.
| Address |
|
Array Space |
| 2000 |
-------------> |
A[0] |
| 2004 |
-------------> |
A[1] |
| 2008 |
-------------> |
A[2] |
| --- |
-------------> |
... |
| 2000+ (n-1) |
-------------> |
A[n] |
Define a int[] array, the first address is the 2000,int type 4 bytes, so a[0] the first address 2000,a[1] is 2004
Each query can be as long as an offset, so the query efficiency is high
Reasons for the low efficiency of adding and deleting:
| Address |
|
Array Space |
| 2000 |
-------------> |
A[0] |
| 2004 |
-------------> |
(new Element) |
| 2008 |
-------------> |
A[1] |
| --- |
-------------> |
... |
| 2000+ (n-1) |
-------------> |
A[n] |
The new element will cause the subsequent elements to move, so the deletion is inefficient.
High efficiency and low query efficiency
LinkedList bottom of the list with a two-way circular list, the storage feature of the linked list is not next to each other, it stores each element into three segments: the address of the previous item, the address of the next item, the content of the element.
Each element is arranged in memory as if it were random, so it is very slow to find the element based on the address.
Additions and deletions quickly because, delete an element, the front and back elements will automatically connect, and delete an element only affects the front and back elements, so the efficiency is high.
TCP Three-time handshake, and why not two or four times
- Three-time handshake process:
First handshake: The client sends the TCP packet, the SYN flag bit is 1, the initial ordinal x is saved in the header sequence number (SEQ).
Second handshake: The service-side response Confirmation packet, the SYN flag bit is 1, the ACK is x+1, the initial serial number Y, saved in the header of the serial number.
Third handshake: The client confirms the service-side confirmation packet, the SYN flag bit is 0, the ACK is y+1, and the serial number is Z.
After the second handshake, the server sends the request to the client, the server thinks the connection is successful, but if the client is actually confiscated, the client will assume that the connection is not established, the server will save the necessary resources for the established connection, and the server will crash if a large number of such cases occur.
Pointless fourth time.
Binary tree depth, nodal point
There are at most 2^ (i-1) nodes in the first layer of the binary tree;
The two-fork tree with a depth of K has at most 2^k-1 nodes;
For any binary tree T, if its terminal node number is N0, the number of nodes with a degree of 2 is n2, then N0 = n2 + 1.
The stability of the sorting algorithm is defined as
- After sorting, the data with the same value can be kept in the relative position in the original order.
HTTP method
Get interface Information
Support for Idempotent Post
Emergency view HTTP header for interface
Submit data to the server
Delete a resource on a server
To view supported methods
Common HTTP corresponding status codes
1xx:指示信息--表示请求已接收,继续处理2xx:成功--表示请求已被成功接收、理解、接受3xx:重定向--要完成请求必须进行更进一步的操作4xx:客户端错误--请求有语法错误或请求无法实现5xx:服务器端错误--服务器未能实现合法的请求
101 – 切换协议201 – 已创建。202 – 已接受。203 – 非权威性信息。204 – 无内容。205 – 重置内容。206 – 部分内容。302 – 对象已移动。304 – 未修改。307 – 临时重定向。401 – 访问被拒绝403 – 禁止访问404 – 未找到405 – 用来访问本页面的 HTTP 谓词不被允许(方法不被允许)406 – 客户端浏览器不接受所请求页面的 MIME 类型。407 – 要求进行代理身份验证。412 – 前提条件失败。413 – 请求实体太大。414 – 请求 URI 太长。415 – 不支持的媒体类型。416 – 所请求的范围无法满足。417 – 执行失败。423 – 锁定的错误。500 – 内部服务器错误。200:请求被正常处理204:请求被受理但没有资源可以返回206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围 的资源。301:永久性重定向302:临时重定向303:与302状态码有相似功能,只是它希望客户端在请求一个URI的时候,能通过GET方法重定向到另一个URI上304:发送附带条件的请求时,条件不满足时返回,与重定向无关307:临时重定向,与302类似,只是强制要求使用POST方法400:请求报文语法有误,服务器无法识别401:请求需要认证403:请求的对应资源禁止被访问404:服务器无法找到对应资源500:服务器内部错误503:服务器正忙
Reflection mechanism function
- Gets the class to which an object belongs
- Get all the member variables and methods of a class
- Creating objects at run time
- Methods for calling objects at run time
How Java Creates objects
- New One
- Reflection mechanism
- Clone () method
- How to Deserialize
Java Program Initialization Order
- Parent class static variable
-
Parent class static code block
- Sub-class static variables
-
Sub-class static code block
- Parent class non-static variable
- Parent class non-static code block
-
Parent class Constructors
- Sub-class non-static variables
- Sub-class non-static code block
-
Sub-class constructors
Volatile
Used to modify variables that are accessed and modified by different threads
A variable defined by a volatile type that is taken directly from memory every time it is used by the system and does not take advantage of the cache.
Java Heap Overflow
publicclass HeapOOM1 { staticclass OOMObject{} publicstaticvoidmain(String [] args){ new ArrayList<OOMObject>(); while (true){ list.add(newOOMObject()); } }}
Single-Case mode
The singleton pattern is to create only one object of that class in the application. Also divided into a hungry man mode and lazy mode. * Implement routines that provide only private constructors, and then provide the public getinstance method. *
- A hungry man mode: The object is created from the beginning
- Lazy mode: Creates the object when it needs to be used
A Hungry man mode
publicclass Singleton { privatestaticnewSingleton(); privateSingleton(){}; publicstaticgetInstance(){ return singleton; } }
Lazy mode
publicclass Singleton { privatestatic Singleton singleton; privateSingleton(){}; publicstaticgetInstance(){ ifnull) newSingleton(); return singleton; } }
Multithreading synchronized
publicstaticsynchronizedgetInstance(){ ifnull) newSingleton(); return singleton; }
Double Test lock
publicclass Singleton { privatestaticvolatile Singleton singleton; publicstaticgetInstance() { ifnull) { //Single Checked synchronized (Singleton.class) { ifnull) { //Double Checked newSingleton(); } } } return singleton; }}
Java Basics-3