1. object-oriented and process-oriented differences?
object-oriented Programming provides the form of objects, provides methods to client programmers, masks internal implementations, is transparent to client programmers , and encapsulates methods with Data. The object of things, more in line with human cognition
process-oriented programming is mainly to provide a variety of methods for human use, there is no necessary link between the method and Data. ?
2. object-oriented four basic features (abstraction, encapsulation, inheritance, Polymorphism)
Inheritance: The subclass inherits from the parent class, which greatly improves the reusability of the Code.
encapsulation: binds data to methods so that the outside world cannot manipulate the data directly ?
polymorphic : Run-time polymorphism and Compile-time polymorphism?
Run-time Polymorphism: executes your own method by referencing the method of the specific object being referred To. ?
Compile-time polymorphism: Overloading of methods?
The difference between 3.Overload and override
overriding is a child class override of a method of a parent class for a Parent-child class ?
overloading is for the same class, the same name method in the same class produces different behavior depending on the parameter .
4. Whether the constructor constructor can be override
Can not be rewritten?
5. Access Control public,protected,private, and the default difference
public:all?
protected:? under the same package or subclass can access
Private: the same class?
Default: Package access rights control, same package ?
6. Can I inherit the String class
no, You String is the final class?
The difference between 7.String and stringbuffer, stringbuilder?
String is an immutable string, StringBuilder and StringBuffer are mutable, and StringBuilder is not Thread-safe StringBuffer is Thread-safe.
The relationship between the 8.hashCode and the equals method
When the Equals method of two objects returns true, the hashcode values of the two methods are equal or the efficiency problem will arise
9. The difference between abstract classes and interfaces
Abstract classes can contain member variables and member methods, as well as abstract methods
The interface can contain only constants and abstract methods
Automatic box packing and unpacking
converting base type data To reference type data is a feature after jdk1.5
What is generics, why to use and generic erase
The essence of generics is the parameterized type, the benefit of generics is checking type safety at compile time , and all casts are automatic and implicit , To increase the reuse rate of your Code. before generics are added, the arguments are type object and are prone to classcastexception. Generics are compiler-approved, but virtual machines are not aware, so at compile time, the compiler converts generics to original type erase to replace the generic type with its parent class, such as String into Object and so On. In fact, when using a forced type of conversion, but this is a more secure conversion, because in the compilation phase has ensured the consistency of the Data.
collection classes and diagrams in 12.Java
Collection:list,set,queue
Map:hashmap,hashtable
13.HashMap Implementation Principle ( See source code ) HashTable Implementation principle ( See source code ) (read about It)
14.HashMap and HashTable differences
HashMap is thread insecure, but efficiency is higher than HashTable ,HashMap allows null values and HashTable null values are not allowed
15.HashTable How to implement thread safety ( See source code , add a synchronous lock to a method that may cause thread safety Problems) ArrayList and vectors difference ( See the source code )(see Section)
16.ArrayList and linkedlist differences and usage scenarios
ArrayList is based on the implementation of the array, random access fast, random access to reach O(1), Adding and removing elements consume large
LinkedList is based on a linked list, and adding and removing element blocks is fast, but accessing the elements requires traversing the linked list
The difference between 17.Collection and collections
Collection is a collection interface and collections is a collection of tool classes that provide a series of Collection -based Operations
18.Concurrenthashmap Implementation Principle ( See source code , lock part understanding barrier, need to Learn)
Http://www.cnblogs.com/ITtangtang/p/3948786.html
through the analysisHashtable'll know,synchronizedis for the entire sheetHashtable, that is, each time the entire table is locked to allow the thread to monopolize,Concurrenthashmapallowing multiple modifications to be performed concurrently, the key is the use of lock separation technology. It uses multiple locks to control theHashchanges to different parts of the Table. ConcurrenthashmapInternal Use segment(Segment)to represent these different parts, each segment is actually a smallHash Table, they have their own locks. As long as multiple modification operations occur on different segments, they can be performed concurrently.
Some methods require cross-sections, such asSize ()and theContainsvalue (), they may need to lock the entire table rather than just a segment, which requires all segments to be locked sequentially, and the locks for all segments are released sequentially after the operation is Complete. Here"by order"is very important, otherwise there is a high likelihood of deadlock,Concurrenthashmapinside, the segment array isFinal, and its member variables are actuallyFinal, however, simply declaring the array asFinalarray members are not guaranteed to beFinal, which requires the assurance of Implementation. This ensures that no deadlocks occur because the order in which the locks are obtained is Fixed.
19.Error,Exception Difference
Error is generally a major error, is not controllable, mostly virtual machine problems, encountered such problems are mostly to let the program terminate
Exception refers to anomalies, which appear to help programmers better identify potential problems and better programming. the Exception solution is to catch the exception and keep the program running .
20.Unchecked Exception and Checked Exception, each listing several
Unchecked Exception including Error and runtimeexception
stack overflow, insufficient memory, and so on error
Run-time exception: null pointer exception, array out of bounds, divisor is zero, etc.
Checked Exception need to be surrounded by try-catch exception handling blocks for exception handling
21.java agents,jdk and cglib Dynamic Agents
A proxy is a design pattern through which a delegate can invoke a specific method of delegating, the role of an agent: providing a proxy to other objects to control access to the Object.
In some cases, an object does not want or cannot directly reference another object, whereas a proxy object can act as an intermediary between the client and the target Object.
The idea of proxy mode is to provide additional processing or different operations to insert a proxy object between the actual object and the Caller. These additional operations typically require communication with the actual object.
The dynamic proxy of the JDK is through the interface, and the way to cglib The dynamic proxy is to change the byte code of the class at run Time.
How to implement Multi-threading
Inheritance Thread class, implementing Runnable interface, thread pool
23. State Transitions for threads
run- to - run block
Block - to-run
Run - to-run
24. How to stop a thread
The stop method comes with thread , but this method may cause a thread to deadlock, so it is not recommended.
To end a thread with an exit flag
use interrupts when the interrupt flag is true when the thread is retired ( if the threads are calling wait (), wait (long), or wait (long) in the Object class , Int) method, or the join () of the class, join (long), join (long, int), sleep (long), or sleep (long, int) methods are blocked in the process, The interrupt state will be cleared and it will also receive a interruptedexception.
so you can't simply rely on the isintrrupted method to exit the thread
What is thread safety
Accessing the same block of code in a multithreaded environment does not produce different results, and writing Thread-safe code relies on thread Synchronization.
How to ensure thread safety
Let's see why there are thread insecurity issues:
The JVM has main memoryand Working memory (workingmemory), which is what is normally called java heap memory, stored in the program all class instances, static data and other variables are thread-shared, while working memory is stored in the main memory from the variables and access methods obtained by the local variables, each thread is independent of all, other threads can not access.
thread safety through lock mechanism,synchronize sync lock, lock Lock
The difference between 27.synchronized and Lock
Synchronized may not be as efficient as lock, but the code should also focus on one aspect of readability,Synchronized readability, and the need to release the lock itself, and lock needs to release the lock on its own Initiative.
Lock can resolve issues such as Busy. Other threads can only choose to wait because the lock is not actively released after synchronized locks, such as blocking or sleep methods.
The Lock method in lock waits when the lock is not reached, while trylock is not
1)Lock is an interface, and synchronized is a keyword in Java, Synchronized is a built-in language implementation;
2) synchronized lock In the event of an exception, if you do not actively pass the unlock () to release the lock, it is likely to cause a deadlock phenomenon, so use the lock finally release lock in block;
3)Lock allows the thread waiting for the lock to respond to interrupts, while synchronized does not, while using synchronized , the waiting thread waits until it is not able to respond to interrupts;
4) lock can be used to know if there is a successful acquisition of locks, and synchronized is unable to do .
5)Lock can improve the efficiency of multiple threads for read operations
multi- Threading How to interact with information
The wait method leaves the thread in a wait state and releases the lock until the notify method or Notifyall method of the object wakes the waiting thread.
The difference between 29.sleep and wait ( The direction of the investigation is whether the lock will be released )
The Sleep method does not release the lock, and the wait method releases the lock
Multiple threads and deadlocks
Deadlock caused by improper order of multi-threaded access
31. How to create a deadlock
1. Insufficient Resources
2. Thread running in an inappropriate order
3. Improper allocation of resources
What is a daemon thread and how to implement a daemon thread?
The current thread ends and the daemon thread ends, and the daemon is used to serve the other THREADS.
Set Setdaemon to true before running
33.Java thread pooling technology and principles
The thread pool creates a series of threads beforehand, passes the task to the thread through the request queue, and then processes the result through the result queue
Thread pooling technology improves thread reuse, but increases the risk of deadlocks, consumes memory, requests overload, and thread leaks
34.java and contract concurrent and common classes
The concurrent class is used for concurrent programming
Use of 35.volatile
If these simple data types are declared volatile, the operations on them become atomic level
when the value of a variable is determined by its own previous decision, such as n=n+1,n++ , and so on,thevolatile keyword is invalidated, The action on a variable is atomic level only if its value is independent of its previous value, such as n = m + 1, which is the original level. So be cautious when using volatile key, If you are not sure, you can use synchronized instead of volatile .
Java Knowledge Summary on