java interview questions several basic stage

1. say your understanding of object-oriented

In my understanding, is a natural extension of object-oriented model to the real world, this is a kind of "Everything Is an Object" programming ideas. Any object in real life can be classified as a class thing, but each individual is an example of a class of things. Object-oriented programming is object-centric, message-driven, the procedure + = Object message.
There are three characteristics of object-oriented, encapsulation, inheritance and polymorphism.
The package is a class attributes and behavior of a class of things into abstract, privatization of its properties, the behavior of the disclosure, while improving the confidentiality of data, so that the code modular. This is done so that a higher code reusability.
Inheritance is a further class of things are common attributes and behavior into an abstract superclass and each subclass is a special parent - have behavior and properties of the parent class, also has its own unique behavior and attributes. Doing so extends the existing code blocks, to further improve the reusability of code.
If the encapsulation and inheritance is to make the code reuse, the polymorphism is the interface to achieve reuse. A major role is to decouple polymorphism - in order to release the parent-child class inherits the degree of coupling. If the HAS-A relationship between the parent-child relationship class inheritance relations IS-A, then the class interface and implementation. Briefly, polymorphism is allowed to reference the parent class (or interface) directed subclass (or implementation class) object. Many design patterns are based on object-oriented design polymorphisms.

2.JVM memory area and the GC algorithm
https://blog.csdn.net/anjoyandroid/article/details/78609971
dimensional space: jdk1.8 canceled the permanent generation added element space, and the method area on yuan space

Various interfaces and classes implemented in the set of frame 3 which, respectively, so what characteristics

https://blog.csdn.net/sdgihshdv/article/details/72566485
https://blog.csdn.net/suifeng629/article/details/82179996
https://blog.csdn.net/C18298182575/article/details/87167323

4.string class so what characteristics, what are commonly used API

1.String objects using class equivalence determination equals () method is completed, the "==" to achieve numerical comparison address is
2. Once the string declared content is not changed, changing the content was String class object reference relationship implementations rely on changes of.
There are two instances of class 3.String manner using direct assignment may not produce garbage space and automatically into the pool, do not use assignment constructor.

the indexOf () subscript retrieves a character string or a certain character.
lastIndexOf ()
and indexOf is similar, but find the location of the last to appear.
str.lastIndexOf (str, index), the subscript index look forward position emerging from the last
returns a substring of a string of the substring ()
the charAt (index) returns the character index corresponding
trim () to remove the character string before and after space
startsWith () / endsWith () detects whether the string has been developed beginning or end of the string, the return value is Boolean
Split () / character string separated string in parentheses, the return value is a string array
....

The difference 5.stringBuilder and stringBuffer?

Speed: StringBuilder> StringBuffer> String
thread-safe: StringBuilder is not thread-safe, while StringBuffer is thread-safe
String: Apply a small amount of string manipulation case
StringBuilder: suitable for single-threaded operating a large number of characters in buffer case
situation of a large number of operations under applicable multithreading in character buffer: StringBuffer

Why StringBuilder is unsafe?
char [] value;
int COUNT;
public AbstractStringBuilder the append (String STR) {
IF (STR == null)
return appendNull ();
int len = str.length ();
ensureCapacityInternal (COUNT + len);
str.getChars (0, len, value, COUNT);
count + = len;
return the this;
}
for count + = len; an atomic operation is not performed while assuming two threads are executed after the counter 10 becomes 11 instead of 12

What is atomic operations:
simple example:
transfer, transferred BlOO A, because of power outages, resulting in an A-to-100, B 100 did not receive, so should roll back to 100 A.
Each thread atomic operation is a multi-threaded execution failed and successful while at the same time, in the two threads, since the count inherited from the parent class AbstractStringBuilder, when
the execution of one thread coun + len, another thread to get the count value is still original count value after it len + and a thread on the results obtained, as
it is not thread-safe
and stringBuffer of source:
@Override
public the append the synchronized the stringBuffer (String STR) {
toStringCache = null;
super.append (STR);
return the this;
}
when a thread will be locked immediately after the visit append, so another thread can not access the append method, it is thread-safe
in a multithreaded, stringBuffer in each thread needs frequent lock unlock operation, requiring a longer run time, although stringBuilder do not need to lock unlock,
when due to the thread-safe, more suitable for single-threaded

three ways to create a thread 6, the difference between the API and which threads have blocked?

Runnable, the thread, by Callable and Future create a thread.
1. inheritance Thread class to create a thread, and implement the run method (the thread function needs to be done); constructing subclass object, start () start the thread
2. Implement Runnable interface to create a thread, implement Runnable, implement run () method; Runnable interface class object passed as an argument to the Thread class object and calls start () method;
3. implement Callable interface to create a thread, define a class that implements Callable rewrites the call () method, call () returns a value; perform two kinds of ways:
1) by FutureTask perform object creation Callable implementation class, and passed as a parameter to FutureTask, as FutureTask. Object parameter passed to the thread class, and execution start () method;
2) by means of a thread pool to execute, create a thread pool, and then call the submit method of the thread pool, and Callable achieve column as a parameter,

method II benefits:
1. You can implement a class Runnable objects passed to multiple threads for multiple programming process is the same program code with a resource
2. thread class to create a thread is the use of inheritance, rather only single inheritance in Java, If you need to create a sub-class of the thread can only adopt or implement Runnable interface Callable interface implementations.

advantages of the three methods:
1. the return value
2. call () can Abnormal
3. Run a Future Callable task can be fulfilled, showing the results of an asynchronous computation. It provides a method for detecting computation is complete (isDone ()) to wait for the completion of the calculation, and the calculation of the search result.

Thread blocking API:
SLEEP (); the method specified in ms allows a period of time as a parameter, so that the thread enters the blocked state within the specified time, CPU time can not be obtained, the specified time elapses, the thread may perform re-enter status.
Suspend () and resume () method; supporting the use of, suspend () so that the thread into the blocked state and does not automatically recover, it must be a corresponding resume () call, we can make the thread into an executable state.
yield ( ); so that thread to give the current share of CPU time, but not the thread is blocked, that thread is still in an executable state;
wait () and notify () method; supporting the use, if the wait () has parameters, the equivalent of sleep (but can forcibly wake), wait () parameters not notify, corresponding to suspend (), need to notify wakeup

sleep (0) and sleep (. 1) and not the difference between the sleep:
sleep (0), if the run queue thread scheduler can have greater than or equal to the current thread priority ready thread is present, the operating system saves the current thread from the processing remove the device, the scheduling of other high-priority thread ready to run; if no runnable queue of ready threads or all priority ready thread are lower than the current thread priority, then the current thread will continue, as there is no call Sleep (0) the same.
Sleep (1), will lead to a thread context switch: The calling thread will be removed from the period of time may run queue thread scheduler, this period is approximately equal to the length of time specified by the timeout. Why is approximately equal to it? Because the sleep time in milliseconds, which is related to the time accuracy of the system. Typically, the time accuracy of the system is 10 ms, then specify any less than 10 ms but greater than 0 ms sleep time, will evaluate to up 10 ms.

7. The difference between abstract classes and interfaces? Why even have an abstract class interfaces?

① a class can implement multiple interfaces but can only inherit from an abstract class derived from the abstract class subclass can implement the same interfaces, so that we can draw a conclusion: the interface is a Java implementation of multiple inheritance exists
there may be non-abstract methods ②. abstract class may be the interface of non-abstract methods do not exist, and the side of the interface method is only a declaration, must be modified with public abstract, no concrete realization
③. member variables abstract methods may modified different modifiers, and member variables default interface is static constant
④. abstract class is an abstract object, and the interface is a code of conduct, and this is more important.
(so why have interfaces there are also abstract class)

8. The bubble sort, selection sort, quicksort (Learn)

Bubble sort: What is bubbling? For example, a number of randomly generated underwater bubbles, bubbling up together, to take up the lighter faster bubble
particular: 1234107867
If in ascending order: first comparator 67 and 78, 67 is smaller than 78, sequentially Comparative forward, the front small discharge, a discharge playing back, this is a sort, and then repeating the process a new array, a total ordering wheel n (n is the number of elements);

Select Sort: Pick the smallest element in the array and the first exchange position, then remove the first one to find the smallest element in the array and placed first in the new array, from one array
to repeat this operation.
1234107867
first round: 10 | 34 12 78 67
Second round: 10 12 | 34 78 67
third round: 10 12 34 | 78 67
fourth round: 10 12 34 67 | 78
ends sorting

Quick Sort: Based on radix sort. A first take any base, the first element of the array generally (since when the order of the first element cause the minimum value (maximum value) error occurs, it sometimes takes an intermediate element), and will be smaller than the number of base Within an array, a large number of base than as an array, then two new arrays are sorted recursion.
By the process of the base is divided into two arrays: 12341078678 arr array is assumed
to take a base taken temp = 12 low = 0 (a first array), high = 5 (the last bit array)
in the first round: first step: after the start Previous comparison: arr [high] = 8 < 12 = temp, the end of this step, high and low constant. If there arr [high]> 12, so that the high-1 to give the new high ARR [high] compared with temp, and so on until arr <temp, high change this situation [high], low unchanged.
Step two: then compare with the temp, the same principle and the first step will go from arr [low], as arr [1]> temp, this time low = 1, the end of this step.
The third step: switching arr [low] and arr [high] value of
the first round results: 12 8 10 78 67 34 ( low = 1, high = 5)
Second round: as the first round, the first step, from arr [high] forward, until arr [2] = 10 <12 , this time high = 2, the end of this step
the second step, the arr [low] back, 12,8,10 not greater than 12, to when here, because low = 2 = high, it is obtained by comparing the index index = low = high = 2
second round results: 12810786734
because the value obtained index 3, the ARR [index] as the boundary point Finally, a resulting array [12810786734] is divided into two arrays [12,810] and [786,734]
Two new arrays to repeat the above operations
[12810] -> 12 because the maximum value, so the intermediate value 8 -> [8] and [1012] -> [8], [10], [12]
[786734] -> is 67, -> [34], [67 78] -> [34], [67], [78] -> [8], [10], [12], [34], [67], [78]
(Development: Hill sort, insertion sort)

9. What is a deadlock? How to avoid deadlocks

Definition of deadlock: the so-called deadlock refers to a deadlock because multiple threads compete for resources caused by (wait for each other), the absence of external force, these processes will not be able to move forward.

Reason:
1) system resources to compete
the conventional system has the inalienable resources, their number is insufficient to meet the needs of multiple processes running, makes the process during operation, due to an impasse over resources, such as tape drives, printers Wait. Only competition for resources, it may not be deprived of deadlock, competition for resources is not deprivation can lead to deadlock.
2) the process of promoting an illegal order
process during operation, request and order the release of resources properly, will also lead to a deadlock. For example, concurrent processes P1, P2, respectively, to keep the resources R1, R2, and the application process P1 resources R2, process P2 when applying for resource R1, both of which are occupied because the required resources blocked.

Four of deadlock conditions:
exclusive conditions: process requires resources (such as printers) is assigned exclusive control, i.e. a period of time a resource is occupied for only one process. At this time if other processes the resource request, the request process can only wait.
Not deprivation: resources obtained in the process before completion is not used, can not be forcibly taken away by other processes, which can only be obtained by the process of the resources themselves to release (only active release).
Request and keeping conditions: the process has been maintained for at least one resource, but proposed a new resource request, the resource has been occupied by another process, this time requesting process is blocked, but the resources that they have available to keep hold.
Loop wait condition: there is a waiting loop chain process resource, each resource chain acquired simultaneously process required for the next process in the chain. I.e., there is a process in a waiting state set {Pl, P2, ..., pn }, where Pi is waiting for a resource P (i + 1) occupancy (i = 0, 1, ... , n-1), Pn waiting for resources to be occupied P0.

Avoid deadlock:
1. locking sequence (thread locking in a certain order)
2. Locking time (when the thread tries to acquire a lock with a certain time limit, the time limit is exceeded request for the waiver of the lock, and the release of their possession lock)
3. deadlock detection
https://blog.csdn.net/ls5718/article/details/51896159

 

Guess you like

Origin www.cnblogs.com/zn19961006/p/11869182.html