1. The difference between overloading and rewriting
Overload occurs in the same class, access modifiers, return type, parameter name, number of parameters, parameter types, parameter order is not exactly the same, but the method must be the same name.
Rewrite occurs different classes present in the inheritance relationship, access modifier is greater than the parent, if the parent class with private access modifiers modification, this method can not be rewritten, the return type is less than the parent class, the same method name, throwing the abnormality is less than the parent class, the list of parameters must be the same.
2. String and StringBu FF ER , the StringBuilder what is the difference? String Why is immutable?
final keyword string class using the modified character string array to hold, private final value []; So string objects are immutable. string class can not be inherited, because the class is final modification.
The StringBuilder and StringBu ff er inherit from AbstractStringBuilder class, in also using an array of characters stored in char AbstractStringBuilder string [] value but not modified by the fi nal keywords, so the two objects are variable. StringBuilder and StringBu ff er constructor is calling the parent class constructor is AbstractStringBuilder achieve.
The String object is immutable, it can be understood as a constant thread safe. AbstractStringBuilder is StringBuilder StringBu ff er and the common parent class that defines the basic operation of some of the string, such as expandCapacity, append, insert, indexOf other public methods. StringBu ff er method adds genlock or method call added a synchronization lock, so are thread-safe. StringBuilder and no method plus genlock, so not thread-safe.
Each time String when the type of change, generates a new String object and the pointer to the new String object.
StringBu ff er each time to StringBu ff er object itself to operate, instead of generating new object and change the object reference. Use the same circumstances
StirngBuilder compared to the use StringBu ff er only get about 10% to 15% performance increase, but bear the risk of multithreading unsafe.
A summary of the use of the three
1. The use of small amounts of data string
2. The operation of the large amounts of data using a single-threaded operating string buffer stringBuilder
The use of large amounts of data operation stringBuffer string buffer 3. multithreading
3. final difference between the static and
final
1. modified class, which can not be inherited
2. modification methods, this method can not be overridden
3. modified variables, once the value of the variable is assigned, can not be modified
static
1. Modified class method, which is called class methods, class object is invoked directly
2. The modified block of code is executed when the class is loaded
3. modified variables, JVM will be allocated on the heap memory space, all references to the program memory space for this variable to it, and will not reallocate space.
4. The sync block synchronization lock and synchronization method
A sync block
1. In order to solve the concurrent operation may be caused by abnormal, Java multithreading support the introduction of a synchronous monitor to solve this problem, the use of synchronous monitor the general method is synchronized block. The syntax is as follows:
synchronized(obj){
Where obj is synchronized monitor, his meaning is: before the thread starts executing synchronized block, must obtain lockstep monitor, any time only one thread can obtain the lock on the synchronization of the monitor, when synchronized block execution completed after the thread releases the lock sync monitor. Although the java program allows to use any object as a synchronization monitor, but the monitor synchronization purposes is to prevent two threads concurrent access to the same shared resource, it is often recommended to use shared resources may be accessed concurrently act as synchronization monitors.
Two synchronization method
Synchronization method is to use a synchronized keyword method, which is the synchronization method modification. The synchronization method (non-static method) without having to explicitly specify the synchronization monitor, synchronization monitor synchronization method is the this, the object of the method is invoked. Can easily be achieved by synchronizing method thread-safe class, thread-safe class has the following characteristics: Each thread after calling any method of the object can get the right results;
1, the synchronization code block, the end of the synchronization method is performed, i.e., the current thread releases the synchronization monitor;
2. The current thread encountered in the sync block, the synchronization method break, return block to terminate the continued execution of the method;
3. The current thread appeared untreated Error or Exception block synchronization code, the synchronization process, resulting in an abnormal end of the block, the method;
2. the following situations, the thread will not release the synchronization Monitor:
2. the following situations, the thread will not release the synchronization Monitor:
4. Description of the similarities and differences of synchronized with java.util.concurrent.jocks.lock
lock API is introduced after Java5
The main similarities, lock all the functions synchronized to complete the implementation:
The main difference: synchronized in the JVM level is a keyword. The lock is a class
There are more accurate than synchronized lock thread semantics and better performance, but does not force the lock must obtain:
Lock locks suitable for mass synchronization synchronization code, synchronized lock for a small amount of code synchronization issues.
.synchronized automatically releases the lock (a thread executing the synchronization code will release the lock; the lock release exception occurs during the execution of the thread b), to be hand Lock release lock (UNLOCK () method releases the lock), or threads readily caused in a finally deadlock;