Daniel interview Ali come back experience summary (want to enter Ali and other large companies to see over)

Three major features of Java

Encapsulation, inheritance, polymorphism

The difference between abstract classes and interfaces

Java Abstract class:

Classes that use keyword Abstract modifiers are called abstract classes.

The method of modifying with abstract is called abstract method.


1 classes that contain abstract methods must be declared as abstract classes (whether or not they contain other general methods) (otherwise, compile passes);

2 abstract classes can be without abstract methods and can have common methods.

3, abstract classes must be inherited, and abstract methods must be rewritten:

If a subclass is an abstract class, it does not need to be overridden; otherwise, it must be overridden (override).

An abstract class cannot be instantiated (you cannot construct an object of that class directly);

Abstract method:

There is no body of methods in the class (abstract methods only declare, and do not need to implement certain functions);

Abstract methods in an abstract class must be implemented;

If a child does not implement the abstract method in the parent class, then the child becomes an abstract class;

Virtual method:

The virtual function exists for polymorphism.

There is no concept of virtual function in Java. Its ordinary function is equivalent to the virtual function of c++, and dynamic binding is the default behavior of java. If Java doesn’t want a function to have virtual function characteristics, you can add the final keyword to a non virtual function.

Java interface:

A statement of a series of methods.

1 only statements were not fulfilled;

2 there are different ways to implement in different classes;

Common ground

1, interfaces and abstract classes are not instantiated; they are at the top of the inheritance tree for implementation and inheritance by other classes.

2, interfaces and abstract classes can contain abstract methods, and implementations of interfaces or classes that inherit abstract classes must implement these methods.


1, interfaces can only contain abstract methods and default methods, which can not provide method implementation for ordinary methods; abstract classes can completely contain ordinary methods.

2, the static method cannot be defined in the interface, and static methods can be defined in the abstract class.

3, only static constants can be defined in the interface; ordinary member variables cannot be defined; abstract classes can either define ordinary member variables or define static constants.

The constructor does not contain 4, interfaces, abstract classes can contain a constructor in the constructor, the abstract class is not used to create the object, but let the sub class calls the constructor to complete the initialization operation belongs to the abstract class.

5, the interface does not contain initialization blocks, but the abstract class can completely contain initialization blocks.

6, a class can have at most one immediate parent class, including an abstract class, but a class can implement multiple interfaces directly, and by implementing multiple interfaces, it can make up for the lack of Java single inheritance.

Stack memory and heap memory, references, and value passing

Stack memory, heap memory

Some basic types of variables defined in the function and the reference variables of the object are allocated in the stack memory of the function. When defining a variable in a code block, Java is on the stack for the variable distribution of memory space, when the scope over variables, Java will automatically release the allocated memory space for the variable, the memory space can immediately be used for other purposes. Heap memory is used for storing objects created by new and array. Allocated on the heap memory management by the Java virtual machine to automatic garbage collector. The array and object become garbage only when no reference variable points to it, and can no longer be used, but still occupies memory and is released by the garbage collector at a subsequent uncertain time. This is also the main reason that Java accounts for memory.

Reference value transfer

Value transfer: when the method calls, the actual parameter passes its value to the corresponding formal parameter, and the change of the formal parameter value in the execution of the method does not affect the value of the actual parameter.

Reference delivery: also known as address. The method call is refers to the actual parameters (address, rather than the value of the parameter) is passed to the corresponding formal parameter method, the method of execution, to form parameters of the operation is actually on the actual parameters of the operation, the actual value parameters change will affect the implementation of the form parameter value method.

Singleton patterns and benefits

The constructor is privatized, and a static method is used to obtain the object instance.


1) a singleton class can only have one instance.

2) a singleton class must create its own unique instance by itself.

3) a singleton class must provide this instance to all other objects.

Major advantages:

1) provides controlled access to unique instances.

2) because there is only one object in the system memory, the system resources can be saved. For some objects that need to be frequently created and destroyed, the single instance model can undoubtedly improve the system performance.

3) allow variable instances.

Major shortcomings:

1) due to simple mode no abstraction layer, so the expansion of the singleton class is very difficult.

2) the responsibility of the single case class is too heavy, and to a certain extent, it violates the principle of single responsibility”.

3) the abuse of single case will bring some negative problems, such as resources in order to save the database connection pool design for the singleton class object, may lead to a shared connection pool object program too much and connection pool overflow; if the instantiated object is not a long time to be used, the system will be considered garbage recycling, this will result in the loss of the state of the object.

Overloading and rewriting

Overloading: Overloading

(1) the method overloading of Java means that multiple methods can be created in a class that have the same name, but with different parameters and different definitions. When calling methods, determine the specific method to use, by means of the number of arguments and parameter types passed to them, which is polymorphism.

(2) when overloaded, the method name is the same, but the parameter type is different from the number, and the return value type can be the same or different. Cannot return the type as the criterion for the overloaded function.

Rewrite: Overriding

Note: when rewriting a parent class method, use the @Override tag to alert the compiler to check if the code is rewritten, rather than overloading the original method.

(1) polymorphism between a parent class and a subclass to redefine the functions of the parent class. If you define a method in a subclass that has the same name and argument as its parent, we say that the method is overridden (Overriding). In Java, subclasses inherit methods in the parent class without the need to rewrite the same methods. Sometimes, however, subclasses do not want to inherit the methods of the parent class exactly, but want to make certain changes, which requires rewriting of methods. Method overrides, also called method overrides.

(2) if the method in the subclass has the same method name, return type, and parameter table with a method in the parent class, the new method overrides the original method. If you want the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.

(3) access modifiers for subclass functions must not be less than the parent class.

Conversion and construction order between subclasses and superclass

A conversion between a child class and a parent class:

Subclasses can be automatically converted into superclass types.

When creating child class objects:

First, the constructor of the subclass is called

Call the constructor of the parent class

Execute the constructor of the parent class

Execute the constructor of the subclass

Final, finally, finalize

Final is used to declare properties, methods, and classes that represent immutable attributes, methods that cannot be covered, and classes that are not inheritable.

Finally is part of the exception handling statement structure, which means that it always executes.

Finalize is a method of the Object class, which will call the recovered object when the garbage collector executes, and can override this method to provide other resource recovery when garbage collection, such as closing files, etc.

The difference between Synchronized and volatile

Volatile acts only on variables that can be shared among multiple threads. If a field is declared volatile, the java thread memory model ensures that all threads see the value of this variable consistent. The Volatile variable modifier, if properly used, is less expensive to use and execute than synchronized because it does not cause switching and scheduling of thread contexts.

Synchronized gets and frees monitors – if two threads use the same object lock, the monitor can force the code block to be executed by only one thread – a fact known to all. However, synchronized also synchronizes memory: in fact, synchronized synchronizes the memory of the entire thread in the main memory area. So volatile only in the “main” memory memory and thread synchronization between the value of one variable, while synchronized by locking and unlocking a monitor to synchronize all the values of the variables. Obviously, synchronized consumes more resources than volatile.

1) volatile told JVM nature of the current variables in registers (working memory) in value is uncertain, you need to read from the main memory, no mutex is locked; synchronized the current variable, only the current thread can access the variable, the other thread is blocked.

2) volatile can only be used at variable levels; synchronized can be used at variables, methods, and class levels

3) volatile only in the “main” memory memory and thread synchronization between the value of one variable, and the locking and unlocking a monitor to synchronize all variables by synchronized; synchronized obviously consumes more resources than volatile.

4) volatile does not cause thread congestion; synchronized can cause thread congestion.

5) volatile markup variables are not optimized by the compiler; synchronized markup variables can be optimized by the compiler.


Collection: represents a set of objects, each of which is its child element.

Set: Collection that does not contain duplicate elements.

List: there are sequential Collection and can contain repeating elements.

Map: an object that maps the key (key) to a value (value), and the key cannot be repeated.

The collection class does not have the reason to implement the Cloneable and Serializable interfaces

The Collection interface specifies a set of objects, whose elements are its objects. How to maintain these elements is determined by the specific implementation of the Collection. For example, some Collection implementations such as List allow duplicate elements, while others, such as Set, are not allowed. Many Collection implementation have a public clone method. However, there is no point in putting it into all implementations of the collection. This is because Collection is an abstract expression. The important thing is to realize.

The semantics and meanings of cloning or serialization can play a part when dealing with concrete implementations. So, the concrete implementation should decide how to clone or serialize it, or whether it can be cloned or serialized.

In all implementations, authorization, cloning, and serialization lead to less flexibility and more restrictions. A particular implementation should determine whether it can be cloned and serialized.

What is the importance of the hashCode () and equals () methods?

HashMap uses the hashCode () and equals () methods of the Key object to determine the index of the key-value pair. These methods can also be used when we try to get values from HashMap. If these methods are not implemented correctly. In this case, two different Key may produce the same (hashCode) and equals (output), HashMap will think they are the same, then cover them, rather than storing them in different places. Similarly, all collection classes that do not allow storage of repeated data use hashCode () and equals () to find duplicates, so it is important to implement them correctly.

The implementation of equals () and hashCode () should follow the following rules:

(1) if o1.equals (O2), then o1.hashCode (o2.hashCode) = true (always).

(2) if (o1.hashCode) = o2.hashCode (), does not mean that the o1.equals (O2) for true.

The Java in HashMap uses the hashCode () and equals () methods to determine indexes for key values, and these two methods are used when values are obtained from keys. If the two methods are not implemented correctly, two different keys may have the same hash value, so they may be considered equal by the collection. What’s more, these two methods are also used to discover duplicate elements. Therefore, the implementation of these two methods is crucial to the accuracy and correctness of HashMap.

The difference between HashMap and HashTabel

Both HashMap and Hashtable implement the Map interface, so many of the features are very similar. But they have the following differences:

1) HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.

2) Hashtable is synchronous, and HashMap is not. As a result, HashMap is better suited to single threaded environments, while Hashtable is suitable for multithreaded environments.

3) HashMap provides a set of keys that can be supplied with iterations, so HashMap is a fast failure. On the other hand, Hashtable provides Enumeration for keys.

Hashtable is generally considered a legacy class.

Comparable and Comparator interfaces

Comparable and Comparator are used to achieve the set in order, only Comparable is implemented in collection methods defined inside a sort of Comparator is set in the external implementation so as to realize the sorting, sorting, we need to set Comparator interface definition (compare method) or compareTo method to realize Comparable interface in the collection (in).

Comparable supports self comparison, while the latter supports external comparisons;

Comparable is an object that itself supports the self – comparison interface that you need to implement (such as String and Integer, which you can do by yourself)

Comparator is a dedicated comparator that allows you to write a comparator to compare the size of the two objects when the object does not support self comparison or self comparison functions that do not meet your requirements.

That is to say when you need an array of a custom class or set of comparison when you can implement the Comparable interface when you need an array to an existing class or set to compare the time it must implement the Comparator interface. In addition, these two interfaces support generics, so we should define the comparison type while implementing the interface.

How does HashMap work in Java?

HashMap in Java stores elements in the form of key value pairs (key-value). HashMap requires a hash function that uses the hashCode () and the equals () method to add and retrieve elements from the collection / collection. When the put () method is called, HashMap computes the key value of the hash, then stores the key pair on the appropriate index in the collection. If key already exists, value will be updated to new values. Some of the important features of HashMap are its capacity (capacity), load factor (load, factor) and resizing (threshold).

1) HashMap has an internal class called Entry, which is used to store key-value pairs.

2) the Entry object above is stored in an array of Entry called table.

3) the index of table is logically called bucket, which stores the first element of the list.

4) key’s hashcode () method is used to find the bucket in which the Entry object resides.

5) if the two key have the same hash value, they will be placed in the same bucket of the table array.

6) the equals () method of key is used to ensure the uniqueness of key.

7) the equals () and hashcode () methods of the value object are not used at all. Key content

Put: the hash value calculated according to the hashcode () method of the key determines the index of the key in the Entry array.

Get: find one of the elements in the array through hashcode Entry

Implementation of Hashcode

The conventional protocol for hashCode is:

When the hashCode method is called multiple times on the same object during the execution of the Java application, the same integer must be returned uniformly, provided that the information used in the equals comparison on the object is not modified. The integer does not need to be consistent from one execution of an application to another execution of the same application.

If the two objects are equal according to the equals (Object) method, then calling the hashCode method on each object in each of the two objects must produce the same integer result.

When the equals method is overridden, it is often necessary to override the hashCode method to maintain the conventional protocol of the hashCode method, which declares that equal objects must have equal hash codes.

The difference between String, StringBffer, StringBuilder,

1) variable and immutable

In the String class, string strings are saved using the character array. As follows, because you have the final modifier, you can know that the string object is immutable.

Private Final char value[];

Both StringBuilder and StringBuffer inherit from the AbstractStringBuilder class. In AbstractStringBuilder, strings are also saved using character arrays. Here are two objects that are mutable.

Char[] value;

2) is multithreading safe?

Objects in String are immutable and can be understood as constants, which are obviously thread safe.

AbstractStringBuilder is the common parent class of StringBuilder and StringBuffer, defining the basic operations of strings, such as expandCapacity, append, insert, indexOf, and other public methods.

StringBuffer adds a synchronous lock to the method or adds a synchronous lock to the calling method, so it is thread safe

What’s the difference between HashMap, HashSet, HashTable?

The difference between HashSet and HashMap

1) HashSet is an implementation class of set, hashMap is an implementation class of Map, and hashMap is an alternative to hashTable

2) to HashSet objects as elements, and HashMap (key-value) of a group of objects as the elements, and the HashSet refused to accept the duplicate object.HashMap can be viewed as three views: key Set, value Collection, Entry Set. HashSet is actually a view of HashMap.

HashSet internally is implemented using Hashmap, and unlike Hashmap, it does not require two values of Key and Value.

Inserting objects into HashSet is actually done internally

Public, Boolean, add (Object, O) {

Return map.put (O, PRESENT) ==null;


The difference between hastTable and hashMap:

1) Hashtable is based on the old Dictionary class, and HashMap is an implementation of the Map interface introduced by Java 1.2.

2) this difference is the most important point: the methods in Hashtable are synchronous, while the HashMap method (by default) is asynchronous. That is, in multithreaded applications, Hashtable can be safely used without special operations; for HashMap, additional synchronization mechanisms are required.

3) only HashMap allows you to leave empty values as key or value of the entry of a table. There is only one record in HashMap that can be an empty key, but any number of entries can be empty value. That is to say, if there is no search key found in the table, or if the search key is found, but it is an empty value, then get () returns null. If necessary, use the containKey () method to distinguish between the two cases.

Thread safe HashMap – java.util.concurrent.ConcurrentHashMap

ConcurrentHashMap adds the Segment layer, each Segment in principle equivalent to a Hashtable, and ConcurrentHashMap is an array of Segment.

Insert data or read the data to the ConcurrentHashMap, first of all to the corresponding Key mapped to the corresponding Segment, so don’t lock the entire class, as long as the single Segment operation can lock operation. In theory, if you have n Segment, then you can simultaneously support concurrent access of N threads at the same time, thus greatly improving the efficiency of concurrent access. In addition, the rehash () operation is also performed on a single Segment, so the cost of the rehash resulting from the increase in data in the Map is also low.

A blog: thread safe HashMap realization method and principle of http:// liqianglv2005.iteye.com /blog/2025016

The difference between ArrayList and LingkedList

1) ArrayList: List implemented with arrays at the bottom

Features: high query efficiency, low efficiency of add and delete, lightweight thread unsafe

2) LinkedList: the bottom of the two-way circular linked list List

Features: low query efficiency, high efficiency of additions and deletions

Garbage collection mechanism

The garbage collection mechanism of Java is the ability provided by the Java virtual machine to dynamically reclaim the memory space occupied by any reference object in an idle time without timing.

It should be noted that garbage collection is the memory space occupied by the object without any reference, not the object itself.

The two most common methods are reference counting and object reference traversal. Reference count: in this method, each object in the heap (not cited) have a reference count. When an object is created, and assigns the object to a variable, the variable count is set to 1. When any other variable is assigned to a reference to the object, plus 1 count (a = B, B +1 quoted), but when an object is a reference over the life cycle or is set to a new value, the reference count of the object by 1. Any object with a reference count of 0 can be treated as garbage collection. When an object is collected by garbage, the number of objects it refers to is reduced by 1. Object reference: traversal starts from a set of objects, each link along the entire object graph, a recursive determination can reach the object (reachable). If an object cannot arrive from one of the root objects (at least one), it is collected as garbage.

serialization and deserialization

Serialization is the process of converting an object state into a format that can be maintained or transmitted. Relative to serialization is serialization, which converts the stream into an object. These two processes combine to easily store and transfer data.

Serialization purposes

1, to make custom objects persist in some form of storage;

2. Transfer objects from one place to another.

3, make the program more maintainable

When two processes are communicating remotely, each other can send various types of data. No matter what type of data is, it is transmitted over the network in binary form. The sender needs to convert this object into a byte sequence before it can be transmitted over the network, and the receiver needs to restore the byte sequence to the object. The process object into a sequence of bytes is called object serialization. The process of restoring a byte sequence to an object is called object serialization. Speak more directly, the purpose is to transfer the serialization format data across process

What are the mechanisms of Java flexibility?

Reflex mechanism

The difference between Sleep and wait

The 1 and sleep () methods belong to the Thread class. The wait () method belongs to the Object class.

2, the thread does not release the object lock in the process of invoking the sleep () method. When the wait () method is called, the thread will abandon the object lock.

The sleep () method causes the program to pause and execute the specified time to make the CPU available to other threads, but his monitoring status remains the same and the operation will resume automatically when the specified time is up. When the wait () method is called, the thread will abandon the object lock and enter the waiting lock pool waiting for this object. The thread will only enter the object locked pool when it calls the notify () method for this object.

IO and NIO

1 and IO are stream oriented, and NIO is buffer oriented.

Java IO oriented means that each time one or more bytes are read from the stream until all bytes are read, and they are not cached anywhere. In addition, it cannot move data in or around the stream. If you need to move the data read from the stream before and after, you need to cache it into a buffer first. Java NIO’s buffer oriented approach is slightly different. The data is read to a buffer that it handles later and can move around in the buffer when needed. This adds flexibility to the process. However, you also need to check if the buffer contains all the data you need to process. Also, make sure that when more data is read into the buffer, do not overwrite data that has not yet been processed in the buffer.

2, Java, IO’s various streams are blocked, Java NIO non blocking mode.

The various streams of Java IO are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. The non blocking mode of Java NIO enables a thread to send a request to read data from a channel, but it only gets the data currently available and will not get anything if there is no data available at the moment. Rather than keeping the thread blocked, the thread can continue to do something else until the data changes can be read. Nonblocking writing is the same. A thread requests to write some data to a channel, but without waiting for it to be fully written, the thread can do something else at the same time. Threads usually use idle time for non blocking IO to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).

3, the Java NIO selector allows a single thread to monitor multiple input channels, you can register multiple channels with a selector, and then use a separate thread to “choose” channel: can handle the input of these channels, or have been prepared to write channel. This selection mechanism makes it easy for a single thread to manage multiple channels. And Java IO no selectors.


For a fully functional Socket, the following basic architecture should be included, and its working process includes the following four basic steps:

(1) creating Socket;

(2) open the input / output connection to the Socket;

(3) read / write operations on Socket in accordance with certain protocols;

(4) close Socket.

Deep cloning, shallow cloning

Shallow replication merely copies the object under consideration without copying the object it refers to.

Deep replication copies the objects referenced by the object to be copied.

In Java, there is only a single inheritance, and if you want a class to give new features, it is usually implemented using interfaces.

Support multiple inheritance in c++, allowing a subclass to have interfaces and functions of multiple parent classes at the same time.


Both UDP and TCP belong to the transport layer protocol.

TCP protocol: connection oriented, reliable, byte based flow

UDP protocol: connectionless, unreliable, based on message

1, the TCP agreement contains special guarantee delivery mechanism, when the receiver receives data from the sender information, automatically sends a confirmation message to the sender; the sender to continue the transfer of other information only after receiving the confirmation message, otherwise it will have to wait until the acknowledgement is received so far.

Unlike TCP, the UDP protocol does not provide a guarantee mechanism for data delivery. The protocol itself does not make any detection or prompt if datagrams are lost during the delivery from the sender to the receiver. As a result, UDP protocols are often referred to as unreliable transport protocols.

2, as opposed to the TCP protocol, another difference in the UDP protocol is how to receive bursts of multiple datagrams. Unlike TCP, UDP does not guarantee the order in which data is sent and received.

red-black tree

Is a self balance two binary search tree, the tree is a very interesting balanced search tree; when inserted to calculate, ensure the two fork tree balance; if there is a magnitude of 2 N data query, it only needs to query N times.

We apply the following additions to any valid red black tree:

The 1. node is red or black.

The 2. one is black.

3. all leaves (external nodes) are black.

4. the two child nodes of each red node are black. (there are no two consecutive red nodes on all paths from each leaf to the root)

5. all paths from each leaf to the root contain the same number of black nodes.

These constraints force the key attributes of the red black tree: the longest possible path from root to leaf is no more than two times as short as the shortest possible path. As a result, the tree is roughly balanced.


The reflective mechanism refers to the program’s ability to obtain its own information at runtime.

The use of reflection mechanism to achieve database data increase, check examples.

Basic principle: when you save data, take all the attributes of the object that you want to save out, and then put together the SQL statement. When you query, you will pack all the data you inquire into into a Java object.

1) each table of the database corresponds to a POJO class, and each field in the table corresponds to one of the attributes in the POJO class. And the names of the POJO classes are the same as the tables, and the attribute names are the same as the field names, and the case does not matter because the database is generally case insensitive

2) add standard set and get methods for each of the attributes in the POJO class.

The three handshake and the four wave

Three Handshake: first, the Client sends the connection request message, the Server segment receives the connection, replies the ACK message, and allocates resources for the connection. After the Client side receives the ACK message, it sends the ACK message to the Server section and allocates the resource so that the TCP connection is established.

Four handshake:

Since the TCP connection is full duplex, each direction must be switched off separately. The principle is that when a party completes its data sending task, it sends a FIN to terminate the connection in that direction. Receiving a FIN simply means that there is no data flow in this direction, and a TCP connection can still send data after receiving a FIN. First, the closing party will perform the active shutdown, while the other side will perform a passive shutdown.

(1) the TCP client sends a FIN to turn off data transfers from the client to the server.

(2) the server receives the FIN, which sends back a ACK, confirming that the serial number is 1 of the received serial number. Like SYN, a FIN will take an ordinal number.

(3) the server closes the client’s connection and sends a FIN to the client.

(4) the client sends back the ACK acknowledgement and sets the confirmation serial number to receive the serial number plus 1.

(Equals) and = =

The difference between equals and Java, = = 1 value types are stored in the memory stack (the stack), and the reference type variable in the stack is only to store the address of a reference type variable, which itself is stored in the heap.

2, the operation is two = = the value of the variable is equal to a reference type variable represents the two variables in the heap memory address is the same, namely whether the contents of the same stack.

Whether the two variables represented by the 3 and equals operations are references to the same object, that is, whether the contents in the heap are identical.

4, 2 = = is the address of an object, and equals is the contents of 2 objects, obviously, when the equals is true, not true = =.

If you also want to get a high salary in the IT industry, you can join our training camp courses, choose the most suitable courses for yourself, learn the technology, and give your high salary to the famous enterprises after 8 months. Our courses include: Java engineering, high performance and distributed, high performance, easy to understand. Viaduct. Performance tuning, Spring, MyBatis, Netty, source code analysis, and big data. If you want to get paid, want to learn to think, good employment prospects, want to compete with others to gain advantage, to enter the interview but Ali but worried about the interview, you can come, group number: 636532962

Note: adding group requirement

1, with 1-5 working experience, the face of the current popular technology, I do not know where to start, need to break through technical bottlenecks can be added.

2, stay in the company for a long time, easy to live, but job interview rebuffed. Need to study in a short time, job hopping, high salary can be added.

3, if there is no work experience, but the foundation is very solid, the Java working mechanism, commonly used design ideas, commonly used java development framework master skilled, you can add.

4, I feel very cattle B, general demand can be done. But the knowledge is not systematic, it is difficult to continue breakthroughs in the field of technology can be added.

5., Ali Java senior Daniel live broadcast, to explain the knowledge, knowledge sharing, many years of work experience combing and summarizing, with all of you, fully and scientifically establish their own technical system and technical knowledge!

6. trumpet or small white and other groups are not given, thank you.