1. Question: If A and B objects are referenced circularly, can they be GC?
Answer: Yes, virtual machines today basically use accessibility analysis algorithms to determine whether the object survives, rather than simply referring to counting the object. The accessibility analysis algorithm uses a series of "GC Roots" objects (objects referenced in the virtual machine stack, static attribute reference objects) as the starting point. The paths that these nodes search downward are called reference chains. When an object does not have any reference chain connection to GC Roots, the object is proven that the object is unavailable.
2. How is memory overflow in Java caused?
OutOfMemoryError:
(1) The PerGern Space program uses a large number of jars or classes, which makes the Java virtual machine not loading class space insufficient.
Solution: adjust parameters XX:PermSize and XX:MaxPermSize, reduce jar packages, and reduce duplicate loading of classes.
(2) Java Heap Space Java virtual machine creates too many objects.
Solution: Adjust parameters Xms (initial heap size) Xmx (maximum heap size), check for dead loops or unnecessary duplicate objects
(3) unable to create new native Thread The JVM takes up too much memory space, and creating threads in the JVM also requires creating threads in the operating system.
Solution: Resize threads in JVM.
3. String s = "123"; How many objects are generated in this statement?
If there is no "123" in the string pool, an object is generated and placed in the constant pool. If there is "123", 0 objects are generated.
If String s = new String("123") , if there is no in the constant pool, create one in the constant pool and then create one in heap memory.
4. What is the difference between Error, Exception and RuntimeException?
Error and Exception are both subclasses of Throwable, and RuntimeException is a subclass of Exception.
Error is used to indicate that a reasonable application should not attempt to catch errors.
Exception points out the conditions that a reasonable application needs to capture. It is divided into checked exceptions and unchecked exceptions.
RuntimeException is an exception that is not checked, does not require try catch or declared on the method. The main subclasses: NullPointer, Arithmatic, ArrayIndexOutOfBounds, ClassCast.
5. What is the difference between Reader and InputStream?
They are all abstract classes, Reader is used to read character streams (char or String), and InputStream is used to read byte streams (byte arrays).
6. What is the role of hashCode?
hashCode is mainly used for quick searches, such as in HashMap structure, which is used to locate the location of key-value pairs. If two objects are the same, the hashCode must be the same, while the hashCode objects with the same hashCode may not be the same, which is equivalent to putting them in the same box.
7. What is the difference between HashMap and Hashtable?
Thread safety, null value key, efficiency, HashMap (Iterator fast failure iterator), Hashtable (enumerator iterator), and HashMap element position will change over time
8. Can any object in HashMap be used as a key? Are there any requirements for user-defined objects as a key?
Yes, but the key object must be an immutable object. Otherwise, after Entry is inserted into the map, changing the key value will cause the current key value to be inconsistent with the hash value, that is, it does not match the array index and will not be found.
9. Should I use run() or start() to start a thread?
How to solve the problem of synchronization and concurrency in multithreading? What is a daemon thread? What is a daemon thread? What is a method to implement a daemon thread (the meaning of Thread.setDeamon()) How to stop a thread? Explain what is thread safety? Give an example of thread insecure. Explain the role of Synchronized keywords. When a thread enters a synchronized method of an object, can other threads enter other methods of this object?
(1) start
(2) Inherit the Thread class, implement the Runnable interface, and use ExectuorService, Future, and Callable to implement the thread that returns the value.
(3) Synchronization method, synchronization code block, lock
(4) Damon threads provide services for the operation of other threads, such as GC and Thread. setDeamon(true).
(5) Thread.stop() is not recommended (resources will not be released correctly), and interrupts are used to stop threads.
(6) When multiple threads access an object, if the scheduling and alternating execution of these threads in the runtime environment are not necessary, additional synchronization is not required, or any other coordination operations are performed on the caller, and the correct result can be obtained by calling this object, then the object is thread-safe.
(7) Tickets are sold repeatedly.
(8) Synchronize keywords. When modifying static methods, the class is used as the lock object, and only one thread can access this kind of synchronization static methods; when modifying ordinary methods, the object is used as the lock object, and only one thread can access this kind of synchronization ordinary methods; you can also customize the lock object synchronization code block.
(9) The synchronized method cannot be entered, and non-synchronized can be entered.
10. What new features of JDK8 have you learned about and describe the corresponding features with examples?
(1) lambda expression: functional programming, method reference
(2) Stream API
(3) Default method of interface
(4) Improvement of date and time API, adding the DateTimeFormatter method
11. What are the principles for optimizing SQL?
For details, please see a blog in my database classification
12. What is the difference between Serlvet and CGI in the life cycle of Servlet?
Life cycle: class loading, instantiation (constructing objects), initialization (init), service (service), and destroy.
CGI: common gateway interface, common gateway interface, written in Perl language, creates a CGI object for each request.
Servlet: Only instantiation and initialization are required once, and multi-threading.
13. What are the advantages of StringBuffer? Why fast?
Because StringBuffer does not require repeated creation of String objects, but this is not the case.
For example, String s = "a" + "b" + "c" operation compiler will optimize and become String s = "abc"
The String s = s1 + s2 + s3 compiler will also be optimized and become a StringBuilder append operation. However, if it is not spliced at one time + operation, the String object and the StringBuilder object will be generated repeatedly, which will be very low.
14. Do you understand the encryption and decryption algorithm?
Abstract algorithms: MD5 (128-bit), SHA1 (160-bit), is an irreversible process. No matter how large the data is, data of the same length will be generated after the digest algorithm. It can only be cracked through dictionaries.
Symmetric encryption algorithms: DES, AES, the same secret key is used when encrypting and decrypting.
Asymmetric encryption algorithm: RSA, use different secret keys when encrypting and decrypting, and use RSA2 when connecting to Alipay.
15. Solve high concurrency and high load?
(1) Static page consumption is minimal, HTML is static as much as possible, and information publishing system CMS is used to automatically generate static pages in information entry and cache dynamic data that is not often changed on the front end.
(2) CDN, distribute resources such as CSS/JS on different servers.
(3) Load balancing (Nginx).
(4) Cache data that is not often changed (Redis, memcache).
(5) The image server is separated from the application server.
(6) Cluster.
16. Integer internal cache?
There is a static Integer array inside the Integer class, which stores some Integer objects that have been initialized. The general value is (-128~127). If you use == comparison, sometimes false will be returned because the value is not in the cache, so you should use equals to compare.
17. What is the principle of ArrayList?
(1) ArrayList is thread-insecure. If you want thread-safe, use CopyOnWriteList.
(2) The bottom layer is the Object[] array, and there is an elementData reference pointing to the array inside. At the beginning, it points to a cached empty array (transient) by default. When you want to expand, a new array with a size of 1.5 times (x + (x >>1)) will be re-new, and then the old element will be copied to the new array through the System.arraycopy native method.
(3) The algorithm complexity of the random read and write (get, set) method is O(1).
(4) There are two types of addition operations. The algorithm complexity of add(index, value) is O(n), because it needs to be moved through element copying; while the algorithm complexity of add(value) operation is O(1) (if expansion does not occur).
(5) The time complexity of the deletion operation is O(n), because whether it is deleted according to index or object, it is necessary to implement the movement operation through copying. After deletion, the array size will not change, and the length is maintained by the size attribute. When deleting objects by object, you cannot use new objects. You must delete them through references to objects in ArrayList.
18. What is the principle of LinkedList?
(1) The underlying layer is a bidirectional linked list, maintaining a first pointer and a last pointer.
(2) The time complexity of random read and write (get, set) is O(n).
(3) The time complexity of the insertion operation add(object) is O(1); the time complexity of add(index, object) is O(n).
(4) The time complexity of the delete operation remove(object) is O(1); the time complexity of the remove(index) is O(n).
19. Things to note when using thread pools?
(1) Prevent deadlocks. All threads in the thread pool are waiting for event A to occur, and no free threads to execute A.
(2) Prevent insufficient system resources. To control the number of threads.
(3) Prevent concurrent errors.
(4) Prevent thread leakage. A thread terminates abnormally due to RuntimeException or Error not being caught normally, and the thread pool loses a thread.
(5) Avoid task overload.
20. What is the difference between @Autowire and @Resource in Spring?
@Autowire is assembled by type by default. By default, it requires that the dependency object must exist. If null is allowed, it can be set to false. If we want to use assembled by name, we can use it in conjunction with the @Qualifier annotation;
@Resource is assembled by name by default. When a bean matching the name cannot be found, it will be assembled according to the type. It can be specified through the name attribute. If the name attribute is not specified, when the annotation is annotated on the field, the name of the field is taken by default as the bean name to find the dependency object. When the annotation is marked on the setter method of the attribute, the attribute name is taken by default as the bean name to find the dependency object.
21. Thread state transfer diagram.
22. Methods of Object class?
(1) equals: determine whether it is equal.
(2) hashCode: Find the hash value. If the hash values of the two objects are equal, it may not be equals; if equals, the hash values of the two objects must be equal.
(3) toString: Obtain the string expression form of the object, which is generally rewritten.
(4) getClass: reflection method to obtain the class to which the object belongs.
(5) wait: It can only be used in synchronized code segments. If the time parameter is not included, the executing thread will cause the lock to be abandoned and enter the waiting state (wait pool).
(6) notify: It can only be used in synchronized code segments, take out a thread from the waiting pool and put it into the lock flag pool.
(7) notifyAll: can only be used in synchronized code segments, and all threads are taken out of the waiting pool and put into the lock flag pool.
23. The underlying principle of Java serialization?
Convert object information into bytecode information.
(1) Store the description of serialization. (2) Current class description information. (3) Description of the current class attributes. (4) Superclass description (5) Superclass attribute description (6) Superclass attribute value description (7) Subclass attribute value description