Ali Java Development interview Frequently asked Questions summary 3

Source: Internet
Author: User
Tags ack

Java collection classes

The collection classes for Java are located in the Java.util package, where the Java collection holds references to objects, not to the object itself.

There are three main types of Java collections:
1.Set (SET): Objects in the collection are not sorted in a particular way, and there are no duplicate objects. Some of its implementation classes can sort the objects in the collection in a particular way.
2.List (list): Objects in the collection are sorted by index location and can have duplicate objects that allow objects to be retrieved at the index position in the collection.
3.Map (map): Each element in the collection contains a pair of key objects and value objects, there are no duplicate key objects in the collection, and value objects can be duplicated. Some of its implementation classes can sort the key objects in the collection.
Set, list, and map are collectively referred to as Java collections.
Both list and set inherit the collection interface, and map does not inherit the collection interface.
The main implementation classes of List are: ArrayList class, LinkedList class, Vector class.
The main implementation classes of Set are: HashSet class, TreeSet class, Linkedhashset class.
The main implementation classes of map are: HashMap class, Hashtable class, TreeMap class, Weekhashmap class and Identityhahmap class, etc.

The difference between set and list

1, List,set are inherited from the collection interface, are interface
2, List features: elements are placed in order, elements can be repeated
Set features: Elements are not placed in order, elements are not repeatable (note: The element is not in order, but the position of the element in set is determined by the hashcode of the element, its position is actually fixed)
3, the list interface has three implementation classes: Linkedlist,arraylist,vector;
The set interface has two implementation classes: HashSet (the underlying is implemented by HashMap), Linkedhashset.

The object state of Java in hibernate

In hibernate, there are three states of objects: temporary, persistent, and free.

Temporary state: When new is a physical object, the object is in a temporary state, that is, an area of memory that holds temporary data, and if no variable references the object, it is reclaimed by the JRE garbage collection mechanism. The data stored by this object has no relation to the database, except that the object is converted to a persistent object by using the session's Save or saveorupdate to associate the temporary object with the database and inserting or updating the data into the database.

Persistent state: an instance of a persisted object has a corresponding record in the database and has a persistent representation (ID). After a delete operation on a persisted object, the corresponding record in the database is deleted, and the persisted object no longer has a corresponding relationship with the database record, and the persisted object becomes a temporary state. After the persisted object is modified, it is not synchronized to the database immediately until the database transaction commits. The persisted object is dirty (Dirty) before synchronization.

Free State: When the session is closed, clear, or evict, the persisted object has a persistent identifier and a value consistent with the database corresponding record, but because the session has disappeared, the object is not within the persistence management, so it is in a free State (also known as a "de-tube" state). An object with a free state is very similar to a temporary state object, except that it also contains persistent identities.

Characteristics of database transactions

A database transaction (Transaction) is a series of operations performed as a single logical unit of work, either completely or completely without execution. On the one hand, when multiple applications access the database concurrently, transactions can provide an isolation method between applications to prevent interference with each other. Transactions, on the other hand, provide a way for a database operation sequence to recover from failure.
Transactions have four properties: atomicity (atomicity), consistency (consistency), isolated (isolation), persistence (durability), or acid.
1 atomicity (atomicity)
The atomicity of a transaction is that the operations in the transaction are not split and are only allowed to execute all or not.
2 Consistency (consistency)
Transactional consistency means that the execution of a transaction does not compromise the consistency of the database, and consistency is also known as completeness. After a transaction executes, the database must transition from one consistent state to another.
3 Isolation (Isolation)
The isolation of transactions means that concurrent transactions are isolated from each other and cannot interfere with each other.
4 Persistence (Durability)
The persistence of a transaction means that once a transaction is committed, the state change to the data should be persisted.

Database transaction isolation level and lock mechanism

In order to balance the isolation performance, the SQL92 specification defines four transaction isolation levels: READ UNCOMMITTED, Read committed (reads Committed), REPEATABLE read (Repeatable Read), serialization (Serializable). Four levels are gradually enhanced, and each level resolves a problem at the previous level.
1 read not submitted (READ UNCOMMITTED)
Another transaction modifies the data but has not yet committed, and select in this transaction reads the uncommitted data (also known as Dirty Read).
2 Read submitted (read Committed)
This transaction reads the most up-to-date data (after other transactions have been committed). The problem is that in the same transaction, two times the same select will read different results (non-repeatable read).
Non-repeatable reading refers to the fact that another transaction commits new data during the execution of the same transaction, so that the data results read two times by this transaction are inconsistent.
3 REPEATABLE READ (REPEATABLE Read)
In the same transaction, the result of select is the state of the start point of the transaction, and the same select operation reads the same result. However, there is a phantom reading phenomenon.
Non-repeatable reads guarantee the same transaction, and the result of the query is the state (consistency) at the beginning of the transaction. However, if another transaction commits new data at the same time, when the transaction is updated, the new data is discovered, and it seems that the previously read data is illusory, which is the Phantom reading.
4 Serialization (Serializable)
All transactions can be executed one by one serially, not concurrently.

Selection of Isolation Levels
The higher the transaction isolation level, the greater the consistency of the data, but the greater the impact on concurrency performance, the tradeoff or compromise between consistency and performance.
In general, most applications can choose to set the isolation level of the database to read committed, which avoids dirty reads and can also get good concurrency performance. Although this isolation level results in non-repeatable, phantom-readable, this individual application can be actively locked for concurrency control.

http://www.songjie.info/%E3%80%90%E8%BD%AC%E8%BD%BD%E3%80%91%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%8B%E5%8A%A1%E9% 9a%94%e7%a6%bb%e7%ba%a7%e5%88%ab%e5%92%8c%e9%94%81%e5%ae%9e%e7%8e%b0%e6%9c%ba%e5%88%b6/

HTTP message contains content



TCP/IP three-time handshake and four-time wave

TCP is the host-to-host layer of the Transmission Control Protocol, providing a reliable connection service, using three-time handshake confirmation to establish a connection:
The bit code is the TCP flag bit, there are 6 kinds of marking: SYN (synchronous set up) ACK (acknowledgement acknowledgment) PSH (push transfer) FIN (finish end) RST (reset reset) URG (Urgent emergency)
Sequence number (sequential) Acknowledge number (confirmation)

Three-time handshake

First handshake: Host a send bit code for syn=1, randomly generate SEQ number=1234567 packet to the server, Host B by Syn=1 know, a requirements to establish online;
Second handshake: Host B receives the request to confirm the online information, send an ACK to a number= (host A's seq+1), syn=1,ack=1, randomly generate seq=7654321 packets
Third handshake: Host a after receiving check ACK number is correct, that is, the first sent Seq Number+1, and the bit code ACK is 1, if correct, host A will send an ACK number= (Host B seq+1), ack= 1, Host B is received after confirming the SEQ value and ack=1 The connection was established successfully.
To complete the three handshake, host A and Host B start transmitting data.

Wave four times

The so-called four-time wave (Four-way Wavehand) terminates the TCP connection, that is, when disconnecting a TCP connection, the client and the server are required to send a total of 4 packets to confirm the disconnection of the connection. In socket programming, this process is triggered by executing close on either side of the client or service side,
(1) First wave: The client sends a fin to turn off the client to server data transfer, the client enters the fin_wait_1 state.
(2) Second wave: After receiving fin, the server sends an ACK to the client, confirming that the sequence number is received sequence number +1 (same as SYN, one fin occupies a serial number), and the server enters the close_wait state.
(3) Third wave: The server sends a fin to shut down the server-to-client data transfer, and the server enters the Last_ack state.
(4) The fourth wave: After the client receives fin, the client enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the serial number is received +1,server enter the closed state, complete four waves.

JVM Memory

The JVM allocates memory into the following areas
1:heap
2:stack
3:code
4:static
The JVM's effective management is assigned to these memory areas.
The Code section area contains this bytecode file (byte code)
Stack section (Stack area) contains methods (methods), local variables (locals variables), and reference variables (reference variables)
The heap section (heap area) contains objects (which may contain reference variables (mentioned in the following example))
Static section (static zone) contains data at rest/method (the static data and methods for class methods, subordinate to Class)
1: When a method is called, a frame is created at the top of the stack.
2: Once the method completes execution, the control flow returns the method being called, and the frame in the stack is emptied (flushed)
3: Local variables are created in the stack.
4: Instance variables are created in the heap, are part of the object, and instance variables are attached to the object.
5: Reference variables are created in the stack.
key content about the JVM featured a blog post as follows:
http://blog.csdn.net/tonytfjing/article/details/44278233
Attach one more article:
http://blog.csdn.net/u012152619/article/details/46968883

JVM Garbage Collection

GC Overview for 1.JVM
A GC is a garbage collection mechanism that is used by the JVM to free memory that is consumed by objects that are no longer in use.
The purpose of garbage collection is to clear objects that are no longer in use. The GC determines whether to collect the object by determining whether the object is referenced by the active object. The GC first determines whether the object is ready to be collected. Two common methods are reference counts and object reference traversal.
1) Reference count
The reference count stores all references to a particular object, that is, when the application creates a reference and the reference goes out of scope, the JVM must increase or decrease the number of references appropriately. When an object has a reference count of 0 o'clock, garbage collection is possible.
2) object reference traversal
Earlier JVMs used reference counting, and most JVMs now traverse with object references. Object reference traversal starts with a set of objects and recursively determines the reachable (reachable) object along each link on the entire object graph. If an object cannot arrive from one (at least one) of these root objects, it is garbage collected. During the object traversal phase, the GC must remember which objects can be reached in order to delete the Unreachable object, which is known as the markup (marking) object.
Next, the GC wants to delete the unreachable object. When deleted, some GC simply scans the stack, removes unmarked objects, and frees their memory to generate new objects, called Purge (sweeping). The problem with this approach is that memory is broken into small chunks, which are not sufficient for new objects, but are very large in combination. As a result, many GCs can reorganize objects in memory and compress (compact) to make available space.
To do this, the GC needs to stop other active activities. This approach means that all application-related work is stopped and only the GC is running. As a result, many miscellaneous requests are added and subtracted during the response. In addition, more complex GCS are constantly increasing or running concurrently to reduce or eliminate the interruption of the application. Some GC uses a single thread to do the work, while others use multithreading to increase efficiency.
2. Several garbage collection mechanisms
1) Mark-Clear collector
The collector first loops through the object graph and marks the reachable objects, then scans the stack for unmarked objects and frees up their memory. This collector typically uses a single thread to work and stops other operations.
2) Tag-compression collector
Sometimes also called Mark-purge-compress collector, with the mark-purge collector has the same marking stage. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations.
3) Copy Collector
This collector divides the stack into two domains, often referred to as half-space. Using only half of the space at a time, the new object generated by the JVM is placed in the other half of the space. When the GC runs, it compresses the stack by copying the reachable object to the other half of the space. This method is suitable for short-lived objects, and continuous replication of long-lived objects results in reduced efficiency.
4) Incremental Collector
The incremental collector divides the stack into multiple domains, collecting garbage from only one domain at a time. This can result in a smaller application outage.
5) Generational collector
This collector divides the stack into two or more domains for storing objects of different lifetimes. A new object generated by the JVM is typically placed in one of the domains. Over time, objects that continue to exist will receive a lifetime and go into a longer-lived domain. The generational collector uses different algorithms for different domains to optimize performance.
6) Concurrent Collector
The concurrent collector runs concurrently with the application. These collectors generally have to stop other operations to complete a particular task at some point (such as compression), but because other applications can perform other background operations, the actual time to interrupt other processing is greatly reduced.
7) Parallel collector
The parallel collector uses some traditional algorithms and uses multithreading to perform their work in parallel. The use of multithreading on multi-CPU machines can significantly improve the scalability of Java applications.
  
How the JVM garbage collector works
https://www.ibm.com/developerworks/cn/java/j-lo-JVMGarbageCollection/

JVM Tuning

http://blog.csdn.net/gzh0222/article/details/8363032?cm_mc_uid=52295834175414590497670&cm_mc_sid_50200000= 1459049767

What are the different parts of MVC implemented by those technologies?

MVC is shorthand for Model-view-controller.
The Model represents the business logic of the application (implemented through the JAVABEAN,EJB component); View is the presentation surface of the application (generated by the JSP page);
The controller is the process control that provides the application (typically a servlet).
This design model divides application logic, process and display logic into different components. These components can be interacted with and reused.

AOP: Design Patterns
IOC: Reflection mechanism

ArrayList how does the bottom layer scale?

ArrayList Java bottom-up implementation:

Public boolean Add (E e) {
Ensurecapacityinternal (size + 1); Increments modcount!!
elementdata[size++] = e;
return true;
}
The bottom of the ArrayList Add method is actually an array, and if the array is full it will create a new array larger than the old one, and then copy the old array into the new array.

The realization of HashMap

Several blogs are recommended, which specifically illustrate HashMap:
http://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/

http://my.oschina.net/hosee/blog/618953

Servlet life cycle and how it works

The servlet life cycle is divided into three phases:
1, initialization phase: Call Init () method
2, responding to customer request phase: Call Service () method
3, termination phase: Call Destroy () method
How Servlets work:
The servlet receives and responds to the client request process: First the customer sends a request, the servlet responds to the request by invoking the service () method, is visible through the source code, matches the request in the service () method, chooses to call Doget, Dopost such methods, and then into the corresponding method to invoke the logical layer of the method to achieve response to the customer.

There are no doget, dopost, and so on in the Servlet interface and Genericservlet, these methods are defined in HttpServlet, but all return error information, so, each time we define a servlet, Must implement these methods such as Doget or Dopost.
Each custom servlet must implement the Servlet interface, and the Servlet interface defines five methods, of which the more important three methods involve the servlet's life cycle, respectively, the Init (), service (), destroy () mentioned above. Method. Genericservlet is a generic, non-protocol-specific servlet that implements the Servlet interface. And HttpServlet inherits from Genericservlet, so HttpServlet also implements the Servlet interface. So we only need to inherit httpservlet when we define the servlet.
The servlet interface and Genericservlet are not specific to any protocol, and HttpServlet is a class specific to the HTTP protocol, so the service () method is implemented in HttpServlet and the request is ServletRequest, Servletresponse Strong to HttpRequest and HttpResponse.

Ali Java Development interview Frequently asked Questions summary 3

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.