Interview questions 30-day check-in-day10

1. What is the difference between String, StringBuffer and StringBuilder?

The main differences among String, StringBuffer, and StringBuilder lie in execution efficiency and thread safety.

String: String string constant, which means it is immutable, causing a new String object to be generated every time String is used, and the execution efficiency is low.

StringBuffer: It is a variable class and a thread-safe string operation class. Any operation on the string it points to will not generate a new object. Each StringBuffer object has a certain buffer capacity. When the size of the string does not exceed the capacity, no new capacity will be allocated. When the size of the string exceeds the capacity, the capacity will be automatically increased.

StringBuilder: As a variable string type, the difference between it and String is that StringBuilder can perform operations on strings such as splicing, reverse order, replacement, deletion, and insertion. When performing operations, it will perform operations on the StringBuffer object itself operations instead of generating new objects.

execution speed thread safety scenes to be used
String the worst Safety few string operations
StringBuffer medium Safety A lot of string operations under multithreading
StringBuilder fastest unsafe A large number of string operations under a single thread

2. How is the index implemented in MySQL? What is a B+ tree, the difference between a B tree and a B+ tree, why does MySQL use a B+ tree?

In MySQL, indexes are mainly implemented through B+ trees.

The difference between B+ tree and B tree is:

  1. There are no duplicate elements in the nodes of the B tree (root node/parent node/intermediate node/leaf node), but there are B+ trees.
  2. The intermediate nodes of the B-tree store data pointer information, while the B+ tree stores only the leaf nodes.
  3. Each leaf node of the B+ tree has a pointer pointing to the next node, stringing all the leaf nodes together.

3. What design patterns are used in the Spring framework?

(1) Factory mode: Spring uses the factory mode to create objects through BeanFactory and ApplicationContext

(2) Singleton mode: Bean defaults to singleton mode

(3) Strategy mode: For example, the implementation class of Resource implements resource acquisition strategies in different ways for different resource files

(4) Proxy mode: Spring's AOP function uses JDK's dynamic proxy and CGLIB bytecode generation technology

(5) Template method: You can put the same part of the code in the parent class, and put different codes in different subclasses to solve the problem of code duplication. Such as RestTemplate, JmsTemplate, JpaTemplate

(6) Adapter mode: The enhancement or notification (Advice) of Spring AOP uses the adapter mode, and the adapter mode is also used in Spring MVC to adapt the Controller

(7) Observer mode: The Spring event-driven model is a classic application of the observer mode.

(8) Bridge mode: Different data sources can be dynamically switched according to customer needs. For example, our project needs to connect to multiple databases, and customers will access different databases according to their needs during each visit

Guess you like

Origin blog.csdn.net/qq_56098191/article/details/130300726