Summarize the knowledge points of Java
Final keyword
-Method: cannot be overridden by a quilt class (override)
-Variable: cannot be modified
-Class: Cannot be inherited, derived sub-class
finally keyword
Used in conjunction with the Try/catch statement, even if an exception is thrown. After the try statement block executes, if there is an exception, the catch is executed and then the finally statement block is executed. If the try has a return statement, the finally is executed and then the return is completed.
Finalize method
This method is a method of the object class, before the garbage collector reclaims the object, if the object overrides this method will be called, mainly to do some cleanup action before the object is destroyed, generally does not recommend rewriting.
Overloading and overriding
-Overloading (overloading) means that the method name is the same, the parameters are different, and the return value does not affect overloading.
-Overrides (overriding) have the same method name as the parent class, with the same function signature
The meaning of the interface
Specification, extension, callback (callback is a flexible way to make a specific relationship between classes and classes, like the network in the mobile phone, especially magical)
The meaning of abstract class
The change is separated from the same. The change is an abstract method, which is given to the subclass implementation. The invariant is placed in the parent class unified implementation.
Inner class
-Anonymous inner class (new out of object)
-Static inner classes have no relation to the perimeter class
-Define internal class instances to be bound to external class this
Defining an inner class implements multiple inheritance in a sense, that is, you can inherit from the parent class, and you can use the properties and methods of the perimeter class.
Java Reference Types
- Strong reference: New object, if there are objects holding references, even if the throw oom can not be recycled.
- Soft reference: Recycles when there is not enough memory space.
- Weak references: Whenever a GC occurs, it is recycled.
- Virtual reference: Does not affect recycling, mainly tracking GC, be notified when recycling.
The role of anonymous inner classes
- An inner class can have multiple instances, each with its own state information, and independent of information from other peripheral objects.
- In a single perimeter class, you can have multiple inner classes implement the same interface in different ways, or inherit the same class.
- The time to create an inner class object does not depend on the creation of the perimeter class object.
- The inner class does not have a confusing "is-a" relationship, and he is an independent entity.
- The inner class provides a better encapsulation, except for the perimeter class, where no other class can access
The static method of the parent class can be overridden by the quilt class, why?
No
Subclasses inherit the parent class, using the same static and non-static methods, when a non-static method overrides a method in the parent class (that is, a method override), the static method of the parent class is hidden (if the object is the parent class, the hidden method is called), and the subclass inherits the static and non-static methods of the parent class. As for method overloading I think one of the elements is that in the same class, it is not possible to say what method in the parent class and what method in the subclass is the embodiment of the method overload
The difference between = = and equals in Java, the difference between equals and hashcode
\== is an operator that compares two variables for equality.
Equals, is a method of the Objec class that compares two objects for equality, and the Equals method of the Default object class is to compare the addresses of two objects, just as the \== results. If the objects are compared by \==, compare the memory addresses with the same.
If two objects are equal according to the Equals () method, then the Hashcode method that calls either object in both objects must produce the same integer result.
If two objects are not equal according to the Equals () method, then calling the Hashcode method of any of these two objects does not necessarily produce the same integer result
When you put an object into the collection, you first determine whether the Hashcode value you want to put in the object is equal to the hashcode value of any of the elements in the collection, and if it is not equal, put the object directly into the collection. If the hashcode value is equal, then the Equals method is used to determine if any of the objects in the collection are equal, and if equals does not determine equality, the element is placed directly into the collection, otherwise it is not put.
Back to say get, HashMap also first tune Key.hashcode () count the group subscript, and then see equals if true is found, so it involves equals.
Overwrite equals when always overwrite Hashcode
A common source of error is the failure to overwrite the Hashcode method. In each class that overrides the Equals method, you must also overwrite the Hashcode method. If you do not do this, you will violate the general conventions of Object.hashcode, resulting in the class not working properly with all hash-based collections, such as HashMap, HashSet, and Hashtable.
String StringBuffer StringBuilder differences
A string is an immutable object, so each time a change is made to the string type, it is equivalent to generating a new string object, and then pointing the pointer to the new string object, so it is better to change the content of the strings without string. Because each generation of objects will have an impact on system performance, especially when there are no more references in memory, the JVM's GC will start to work, and the speed will be quite slow.
StringBuffer the object itself, rather than generating a new object, and then changing the object reference. So in general we recommend the use of StringBuffer, especially if the string object is constantly changing.
Java.lang.StringBuffer A variable sequence of characters for thread safety. A string-like buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.
Java.lang.StringBuilder A variable sequence of characters is 5.0 new. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer. The two methods are basically the same
Most cases Stringbuidler > StringBuffer > String
instance the order in which an object executes: Static code block->main Method-construct code block--construction method
The difference between an abstract class and an interface
- Abstract classes can only be single-inherited, interfaces can be implemented more
- Abstract classes can have non-abstract methods, all the methods in the interface are public static
- An abstract class is an is a relationship, and an interface is a relationship of like a
- Abstract classes can have modifier adornments, which are public static by default in the interface
The difference between a process and a thread
-
In short, a program has at least one process, and a process has at least one thread.
-
The thread's dividing scale is smaller than the process, which makes the multi-thread procedure high concurrency.
-
The process has an independent memory unit during execution, and multiple threads share memory, which greatly improves the efficiency of the program operation.
-
Threads are still different from the process during execution. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple threads of execution control.
-
From a logical point of view, the meaning of multithreading is that in an application, multiple execution parts can be executed concurrently. However, the operating system does not consider multiple threads as separate applications to implement scheduling and management of processes and resource allocation. This is the important difference between processes and threads.
-a process is a program with a certain set of independent functions about a single run activity on a data collection, a process that is an independent unit of the system's resource allocation and scheduling.
-
A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has a point in the operation of the necessary resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads that belong to one process.
-
One thread can create and revoke another thread, and can execute concurrently between multiple threads in the same process.
-
The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used. If you are interested in deepening, I suggest you look at "Modern operating system" or "operating system design and implementation." Say a little more clearly about a problem.
Java polymorphism
Object-oriented three major features: encapsulation, inheritance, polymorphism. From a certain point of view, encapsulation and inheritance are almost always prepared for polymorphism. This is our last concept, but also the most important point of knowledge.
-polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)
-The technique of implementing polymorphism, called dynamic binding, is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.
-The role of polymorphism: to eliminate the coupling relationship between types.
-In reality, there are numerous examples of polymorphism. For example, if you press the F1 button, if the current pop-up in the Flash interface is the as 3 help document, if Word Help is currently popping up under Word, Windows Help and Support appears under Windows. The same event occurs on different objects and produces different results. Here are the three necessary conditions for polymorphic existence, requiring you to recite them in a dream!
-
Three necessary conditions for polymorphic existence one, to have inheritance; second, to have the override; the parent class refers to the subclass object.
Benefits of Polymorphism:
1. Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2. expandability (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3. Interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4. Flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5. Simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects.
Implementation of polymorphism in Java: interface implementation, inheriting the parent class for method overrides, and method overloading in the same class.
What causes thread blocking
In order to solve the access violation of the shared storage, Java introduced the synchronization mechanism, now let us examine the access of multiple threads to the shared resources, obviously the synchronization mechanism is not enough, because at any time the required resources are not necessarily ready to be accessed, in turn, the same time the resources prepared may be more than one. To address the problem of access control in this case, Java introduces support for blocking mechanisms.
Blocking refers to pausing a thread's execution to wait for a condition to occur (such as a resource is ready), and the classmate who learns the operating system must already be familiar with it. Java provides a number of ways to support blocking, let's analyze each of them.
Sleep () Method: Sleep () allows you to specify a period of time in milliseconds as a parameter, which causes the thread to enter a blocking state within a specified time, cannot get CPU time, the specified time is over, and the thread re-enters the executable state. Typically, sleep () is used when waiting for a resource to be ready: After the test discovery condition is not met, let the thread block for a period of time and then re-test until the condition is satisfied.
Suspend () and resume () methods: Two methods are used, suspend () causes the thread to enter a blocking state, and does not automatically recover, it must be called by its corresponding resume () to enable the thread to re-enter the executable state. Typically, suspend () and resume () are used when waiting for the result of another thread: After the test finds that the result has not yet been generated, the thread is blocked and the other thread produces the result, calling resume () to restore it.
Yield () Method: Yield () causes the thread to discard the current CPU time, but does not cause the thread to block, that is, the thread is still in an executable state, and the CPU time may be split again at any time. The effect of calling yield () is equivalent to the scheduler thinking that the thread has performed enough time to go to another thread.
Wait () and Notify () methods: Two methods are used, wait () causes the thread to enter the blocking state, it has two forms, one allows to specify a period of time in milliseconds as a parameter, and the other without parameters, the former when the corresponding notify () The thread re-enters the executable state when it is called or exceeds the specified time, and the latter must be called by the corresponding notify ().
At first glance they have nothing to do with the suspend () and the Resume () methods, but in fact they are quite different. The core of the difference is that all of the methods described earlier, blocking will not release the lock (if occupied), and this pair of methods is the opposite.
The core differences above lead to a series of differences in detail.
First, all the methods described earlier are subordinate to the Thread class, but the pair is directly subordinate to the object class, which means that all objects have this pair of methods. At first glance this is very magical, but in fact it is very natural, because this pair of methods when blocking to release the lock occupied, and the lock is any object has, call any object's wait () method causes the thread to block, and the lock on the object is freed. The Notify () method that invokes an arbitrary object causes a randomly selected unblocking in the thread that is blocked by calling the wait () method of the object (but is not really executable until the lock is acquired).
Second, all the methods described earlier can be called anywhere, but this pair of methods must be called in the Synchronized method or block, the reason is simple, only in the Synchronized method or block the current line friend occupy the lock, only the lock can be released. Similarly, locks on objects that call this pair of methods must be owned by the current thread, so that locks can be freed. Therefore, this pair of method calls must be placed in such a synchronized method or block where the locked object of the method or block is the object that invokes the pair of methods. If this condition is not met, the program can still compile, but the illegalmonitorstateexception exception will occur at run time.
The above characteristics of the wait () and notify () methods determine that they are often used with synchronized methods or blocks. A comparison between them and the operating system's interprocess communication mechanism reveals their similarity: the Synchronized method or block provides functionality similar to the operating system primitives, and their execution is not interfered by the multithreading mechanism, which is equivalent to blocks and wakeup Primitives (this pair of methods are declared as synchronized). Their combination allows us to implement an array of sophisticated inter-process communication algorithms (such as semaphore algorithms) on the operating system, and to solve a variety of complex inter-threading communication problems.
The Wait () and notify () methods are finally explained in two points:
First: Calling the Notify () method causes the unblocked thread to be randomly selected from the thread that was blocked by calling the wait () method of the object, and we cannot predict which thread will be selected, so be careful when programming, and avoid problems with this uncertainty.
Second: In addition to notify (), there is also a method Notifyall () can also play a similar role, the only difference is that the call to the Notifyall () method will be called by the Wait () method of the object is blocked all at once unblocked all the threads. Of course, only the thread that gets the lock can go into the executable state.
When it comes to blocking, it is impossible to talk about deadlocks, and a brief analysis reveals that the suspend () method and the call to the Wait () method, which does not specify a time-out period, can generate a deadlock. Unfortunately, Java does not support deadlock avoidance at the language level, and we must be careful in programming to avoid deadlocks.
We have analyzed the various methods of threading blocking in Java, and we have focused on the wait () and notify () methods, because they are the most powerful and flexible to use, but it also makes them less efficient and prone to error. In practice we should use various methods flexibly in order to achieve our goal better.
Collection Frame
ArrayList: A dynamically sized collection that relies on the array to store the underlying. Multithreading is not supported. is used to replace the vector.
Vecter: With ArrayList, support synchronization (synchronised)
HASHMAP: widely used.
HashMap Overview: HashMap is a non-synchronous implementation of a hash table-based map interface. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.
HASHMAP data structure: In the Java programming language, the most basic structure is two, one is an array, the other is an analog pointer (reference), all the data structures can be constructed with these two basic structure, HASHMAP is no exception. HashMap is actually a "chain-table hash" of the data structure, that is, the combination of arrays and linked lists.
The implementation principle of hashmap (very important)
HashMap Source Code Analysis
HashMap Overview: HashMap is a non-synchronous implementation of a hash table-based map interface. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.
HASHMAP data structure: In the Java programming language, the most basic structure is two, one is an array, the other is an analog pointer (reference), all the data structures can be constructed with these two basic structure, HASHMAP is no exception. HashMap is actually a "chain-table hash" of the data structure, that is, the combination of arrays and linked lists.
The difference between HashMap and Hashtable
HashTable thread-safe key and value cannot be null
HashMap non-thread safe, key and value can be null, index in array is more efficient (using hash & lengh-1 modulo)
Jvm
- Garbage collection mechanism
-
Memory model
-
Class loading mechanism
Other
Reflection:
1. Run is the method and properties of the obtained class, interface information.
2. Creating examples
3. Get and Set fields directly from field references, ignoring access modifiers.
From an inheritance standpoint, the constructor is known as private and useful: you can ensure that the class cannot be instantiated externally and only use public static methods to create instances. A singleton pattern is.
Common design Patterns: single-instance mode, builder, adapter, and strategy mode.
Six Principles of design pattern
- Single principle of responsibility
- The principle of the Richter replacement
- Dependency Inversion principle
- Interface Isolation principle
- Dimitri Law
- Opening and closing principle
Wen/maat (author of Jane's book)
Original link: http://www.jianshu.com/p/bd789aff693d
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Java Knowledge Point Review