The latest java interview questions and answers (basic articles)

The latest java interview questions and answers (basic articles)
IT is still a popular industry nowadays, and there are a lot of people interviewing programmers. So, how can I pass the interview smoothly? 2021 latest java interview questions and answers (basic articles), assist your interview!

1. How is the memory overflow in Java caused?

OutOfMemoryError:

(1) PerGern Space A large number of jars or classes are used in the program, so that the Java virtual machine does not have enough class space to load.

Solution: adjust parameters XX:PermSize and XX:MaxPermSize, reduce jar packages, and reduce repeated loading of classes

(2) Java Heap Space The Java virtual machine creates too many objects.

Solution: Adjust Xms (initial heap size) Xmx (maximum heap size), check for infinite loops or duplicate objects created unnecessarily

(3) unable to create new native Thread JVM takes up too much memory space, and creating threads in the JVM also creates threads in the operating system.

Solution: Adjust the thread size in the JVM.

2. String s = "123"; How many objects are generated by this statement?

If there is no "123" in the string pool, an object will be generated and put into the constant pool. If there is "123", 0 objects will be generated.

If String s = new String("123"), if there is no constant pool, create one in the constant pool, and then create one in the heap memory.

3. Question: If A and B objects are circularly referenced, can they be GCed?

Answer: Yes, the current virtual machine basically judges whether the object is alive through the reachability analysis algorithm, rather than through the simple reference counting method to judge whether the object is alive. The reachability analysis algorithm uses a series of "GC Roots" objects (objects referenced in the virtual machine stack, static attribute reference objects) as the starting point, and the downward search path of these nodes is called the reference chain. When an object reaches the GC Roots Without any reference chain connection, the object is proved to be unusable.

4. What is the difference between Error, Exception and RuntimeException, and what are their functions?

Both Error and Exception are subclasses of Throwable, and RuntimeException is a subclass of Exception.

Error is used to indicate errors that reasonable applications should not attempt to catch.

Exception points out the conditions that reasonable applications need to catch. Divided into checked exceptions and unchecked exceptions.

RuntimeException is an unchecked exception that does not require try catch or declaration on the method. The main subclasses: NullPointer, Arithmatic, ArrayIndexOutOfBounds, ClassCast.

5. What is the function of hashCode?

HashCode is mainly used for quick lookup, such as in the HashMap structure, used to locate the position of the key-value pair. If two objects are the same, the hashCode must be the same, but objects with the same hashCode are not necessarily the same, which is equivalent to being placed in the same box.

6. What is the difference between HashMap and Hashtable?

Thread safety, key of null value, efficiency, HashMap (Iterator fast fail iterator), Hashtable (enumerator iterator), HashMap element position will change over time

7. What is the difference between Reader and InputStream?

Both are abstract classes, Reader is used to read character streams (char or String), and InputStream is used to read byte streams (byte arrays).

8. Can any object in HashMap be used as a key, and is there any requirement for a user-defined object as a key?

Yes, but the key object must be an immutable object. Otherwise, changing the key value after the Entry is inserted into the Map will cause the current key value to be inconsistent with the hash value, that is, the array index will not match, and will not be found.

9. Do you use run() or start() to start a thread?

There are several types of multithreading to achieve synchronization and concurrency. How to solve it? What is a daemon thread, and what method is used to realize a daemon thread (the meaning of Thread.setDeamon()) How to stop a thread? The explanation is what is thread safety? Give an example of thread unsafety. Explain what the Synchronized keyword does. 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 threads that return values.

(3) Synchronization method, synchronization code block, lock

(4) Damon thread, providing services for the operation of other threads, such as GC, Thread. setDeamon(true).

(5) Thread.stop() is not recommended (resources will not be released correctly), using interrupts to stop threads.

(6) When multiple threads access an object, if you do not need to consider the scheduling and alternate execution of these threads in the runtime environment, and do not need to perform additional synchronization, or perform any other coordination operations on the caller, call this object The behavior of the object can get the correct result, then this object is thread-safe.

(7) Tickets are sold repeatedly.

(8) Synchronize keywords. When modifying a static method, the class is used as the lock object, and only one thread can access this type of synchronized static method at the same time; when modifying an ordinary method, this object is used as the lock object, and only one thread can access this type of synchronized ordinary method at the same time ; You can also customize the lock object synchronization code block.

(9) The synchronized method cannot be entered, but the non-synchronized method can be entered.

10. What are the principles of optimizing sql in the latest java interview questions?

11. The life cycle of Servlet, the difference between Serlvet and CGI?

Life cycle: class loading, instantiation (object construction), initialization (init), service (service), destruction (destroy).

CGI: common gateway interface, common gateway interface, written in Perl language, creates a CGI object for each request.

Servlet: only needs to be instantiated and initialized once, multi-threaded.

12. What new features of JDK8 have you learned, and describe the corresponding features with examples?

(1) lambda expression: functional programming, method reference

(2)Stream API

(3) The default method of the interface

(4) Improvement of date time API, new DateTimeFormatter method

13. What are the advantages of StringBuffer? why fast?

Because StringBuffer does not need to create String objects repeatedly, but this is not the case.

For example, String s = "a" + "b" + "c" will be optimized by the compiler and become String s = "abc"

String s = s1 + s2 + s3 The compiler will also optimize and become the append operation of StringBuilder, but if it is not a one-time + operation splicing, it will repeatedly generate String objects and StringBuilder objects, and the efficiency will be very low.

14. Do you understand the encryption and decryption algorithm?

Digest algorithm: MD5 (128 bits), SHA1 (160 bits), is an irreversible process, no matter how big the data is, it will generate data of the same length after the digest algorithm. It can only be deciphered through a dictionary.

Symmetric encryption algorithm: DES, AES, use the same secret key for encryption and decryption.

Asymmetric encryption algorithm: RSA, different secret keys are used for encryption and decryption, and RSA2 is used for docking with Alipay.

15. Integer internal cache?

There is a staitic Integer array inside the Integer class, which stores some Integer objects that have been initialized. The general value is (-128~127). If you use == to compare, sometimes it will return false because the value is not in the cache. So it should be compared with equals.

16. The principle of ArrayList?

(1) ArrayList is thread-unsafe. To be thread-safe, use CopyOnWriteList.

(2) The bottom layer is an Object[] array, and there is an elementData reference pointing to the array inside. At the beginning, it points to a cache empty array (transient) by default. When expanding, it will renew a size of 1.5 times (x + (x > >1)), and then copy the old elements to the new array through the native method System.arraycopy.

(3) The algorithmic complexity of random read and write (get, set) methods is O(1).

(4) The addition operation is divided into two types. The algorithm complexity of add(index, value) is O(n), because the movement is performed by copying elements; while the algorithm complexity of add(value) operation is O(1) ( If there is no expansion).

(5) The time complexity of the deletion operation is O(n), because no matter whether it is deleted by index or deleted by object, it needs to be copied to realize the movement operation. After deletion, the size of the array will not change, and it is maintained by the size property length. When deleting by object, you cannot use the new object, and you must delete it by reference to the object in the ArrayList.

17. What is the principle of LinkedList?

(1) The bottom layer is a doubly linked list, which maintains 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 insert 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 remove(index) is O(n).

18. How to solve high concurrency and high load?

(1) The consumption of static pages is minimal, and the HTML is as static as possible. The information release system CMS is used to automatically generate static pages for information entry, and the dynamic data that does not change frequently is cached at the front end.

(2) CDN, which distributes resources such as CSS/JS on different servers.

(3) Load balancing (Nginx).

(4) Cache data that does not change frequently (Redis, memcache).

(5) The picture server is separated from the application server.

(6) CLUSTER.

19. What is the difference between @Autowire and @Resource in Spring?

@Autowire is assembled by type by default. By default, it requires that the dependent object must exist. If it is allowed to be null, you can set its required attribute to false. If we want to use assembly by name, it can be used in conjunction with the @Qualifier annotation;

@Resource is assembled according to the name by default. When no bean matching the name is 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 marked on the field, the name of the field is taken as the bean name by default. Find dependent objects, when the annotation is marked on the setter method of the property, that is, the property name is used as the bean name by default to find the dependent object

20. What are the precautions for using the thread pool?

(1) Prevent deadlock. All threads in the thread pool are waiting for the A event to occur, and there is no idle thread to execute A.

(2) Prevent insufficient system resources. To control the number of threads.

(3) Prevent concurrency errors.

(4) To 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.

Guess you like

Origin blog.csdn.net/seeseeyoua/article/details/128135594