In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the analysis of 21 interview questions in Java, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Analysis of interview questions
1. The underlying structure of Map? (HashMap)
Commentary: the old topic, the interviewer must bear in mind!
Answer: Map stores objects as key-value pairs, and its underlying layer is actually composed of arrays and linked lists. The classic picture is as follows (drawn by others)
When using the put method, first find out whether there is an object in the array position and take the remainder of the array length through key.hashcode; if so, take out the linked list to determine whether there is an object in the linked list with the same key value as the passed key value, and if it exists, the passed value will replace the corresponding value of the linked list key, and if it does not exist, it will be directly added to the linked list through the add () method of the linked list.
When using the get method, first find out whether there is an object in the array location, take the remainder of the array length through key.hashcode; if it does not exist, return empty, if so, traverse the linked list to determine whether there is an object in the linked list with the same key value as the passed key value, and if it exists, the value corresponding to the key value is taken out and returned, and if it does not exist, the return is empty.
2, thread-safe Map (concurrentHashMap) simply said the difference between the two 1.7s and 1.8s. I wanted to ask if I would like to talk about it in depth (source level), but the interviewer said no.
Commentary: the old topic, if you have time, to learn about the various ways to solve HashMap thread safety, as well as the principle! This question can only roughly answer the structural changes, because the implementation code has changed, you can talk about it in detail for a long time, and it is estimated that the interviewer does not have time to listen!
Answer:
Jdk1.7 is implemented by Segment + HashEntry, and the structure is as follows:
The meaning of Segment array is to split a large table into several small table for locking, and each Segment element stores a HashEntry array + linked list, which is the same as the data storage structure of HashMap.
While in jdk1.8,
Implementation of removing Segment + HashEntry + Unsafe
Implementation of changing to Synchronized + CAS + Node + Unsafe
Its structure is as follows:
As shown in the figure above, the Segment field is removed, and what is stored in the array is Node. It is similar to the definition of HashEntry in HashMap, but with some differences. It sets the volatile synchronization lock on the value and next properties, which does not allow you to call the setValue method to directly change the value field of Node.
In addition, the original data structure of table array + one-way linked list is changed to the structure of table array + one-way linked list + red-black tree. When there are too many hash collisions, the linked list will be transformed into a red-black tree.
3. What is the amount of data and concurrency of the project MySQL?
Commentary: this title is the direction of the topic, your answer is different, the direction of the following questions will change.
About capacity: if the number of rows in a single table exceeds 5 million rows or the capacity of a single table exceeds 2GB, it is time to answer the middleware of the database sub-table! The trend of the following topic has become the underlying principle of mycat, sharing-jdbc and other sub-database and sub-table middleware!
On concurrency: if the concurrency number is more than 1200, you should use middleware such as MQ or redis as a compensation measure instead of directly manipulating the database. The following topic goes to the principle of redis and mq!
Since the interviewer is still a fresh graduate, I dare to guess that the interviewer answered in this way.
Answer: the amount of data is estimated to be three or four million, and the concurrent amount is about five or six hundred!
4. What do you know about the database?
Comment: because the amount of data and concurrency you answer is not large, there is nothing to ask about the middleware. Therefore, the topic tends to become the bottom of the database! In addition, this question is a guide question, and the interviewer is giving you the opportunity to lead to what you are best at!
Answer: understand common database tuning methods, index optimization and so on!
5. You talk about the index implementation of the database and the secondary index of non-primary keys.
Comment: this question is based on your answer above! Remember to lead to the basic knowledge of database that you are best at! The default is to answer mysql database.
Answer:
From the point of view of data structure:
B-Tree index, the data structure is a B + tree.
Hash index, Hash index compares the hash value after Hash operation, so it can only be used for equivalent filtering, not for range-based filtering. Almost nothing!
R-Tree index, only supports the geometry data type, also basically does not use!
As for the secondary index of non-primary key, this is actually a non-clustered index! The non-clustered index itself is a Btree, and its root node points to the Btree of the clustered index.
6. The project uses SpringBoot. Can you tell the difference between SpringBoot and Spring?
Commentary: basic questions, will spring boot, basically can answer. Even if you are not prepared, you can answer it by thinking about it on the spot. It also belongs to the guide question!
Answer:
Spring Boot can build independent Spring applications
Built-in containers such as Tomcat,Jetty and Undertow, which means you can just run without having to deploy.
There is no need to configure a bunch of tedious xml files like Spring.
Spring can be configured automatically
Provides some existing features, such as measurement tools, form data validation, and some external configuration and other third-party functions
The POM provided can simplify the configuration of Maven.
7. How is the automatic configuration of SpringBoot done?
Comment: this question is also based on your seventh question, a further question.
Answer:
Why do I need automatic configuration first?
As the name implies, the meaning of automatic configuration is to take advantage of this mode instead of configuring XML tedious mode. In the past, using Spring MVC, you need to configure component scanning, scheduler, view parser, and so on. After using Spring Boot automatic configuration, you only need to add MVC components to automatically configure the required Bean. All implementations of automatic configuration are in spring-boot-autoconfigure dependencies, including automatic configuration of Spring MVC, Data, and other frameworks.
Then answer how spring-boot-autoconfigure depends?
Spring-boot-autoconfigure dependencies work simply by initializing them with the @ EnableAutoConfiguration core annotation and scanning the ClassPath directory to automatically configure class dependencies. For example, there is a Starter component dependency with or without Thymeleaf in the project. If so, get the default configuration according to certain rules and automatically initialize the required Bean.
In fact, you can continue to answer how @ EnableAutoConfiguration annotations work! However, the length is too long, the answer to the above level is enough!
8. How to find the implementation of the interface defined by MyBatis?
Commentary: mybatis underlying principles, to see if you have seen the principles of mybatis. The blogger happened to have written a mybatis himself, so he happened to answer this question.
The blogger's inner activity: "now the school enrollment is so good!"
Answer: there are five steps
1. The Mapper interface is registered in the initial SqlSessionFactory.
2. The Mapper interface is registered in the HashMap named MapperRegistry class, key = Mapper class value = create the factory for the current Mapper.
3. After registering with Mapper, you can get from SqlSession
4. SqlSession.getMapper uses JDK dynamic proxy to generate the proxy object of the target Mapper interface.
5. The proxy class of the dynamic proxy is MapperProxy, where the invocation of the add, delete, modify and query method is finally completed.
9. Java memory structure
Commentary: basic topic, this should be the metropolis of JAVA! Send the score questions! If the blogger understands correctly, he should be asking about the memory structure of JVM!
Answer: there are three main blocks of JVM memory structure: heap memory, method area and stack. Heap memory is the largest piece of JVM made up of the young and the old, while the younger generation is divided into three parts, Eden space, From Survivor space and To Survivor space. By default, the younger generation is allocated according to the 8:1:1 ratio.
The method area stores class information, constants, static variables and other data, which is shared by threads. In order to distinguish it from the Java heap, the method area also has an alias Non-Heap (non-heap). The stack is divided into java virtual machine stack and local method stack, which is mainly used for method execution.
10. Can the object be GC?
Commentary: this question is asking, how can JVM determine whether an object needs to be recycled! Do not need the answer citation counting method, answer reachability analysis algorithm is fine.
Answer:
The basic idea of this algorithm is to search downward through a series of objects called "GC Roots". The path of the search is called reference chain. When an object is not connected to GC Roots, it proves that the object needs to be recycled.
As shown in the figure:
In the figure above, the o3meno4 object does not have any GC Roots to achieve. All these two objects are not available and need to be recycled by GC.
Java can be used as an GC Roots object, including the following:
Objects referenced in the virtual machine stack
Objects referenced by class static properties in the method area
Objects referenced by output in the method area
Objects referenced by JNI in the local method stack
11, Minor GC and Full GC
Commentary: basic questions, JVM tuning, basic metropolis! I'm just wondering why I didn't ask Major GC. Let's answer Major GC, too.
Answer:
Heap memory is the largest piece of JVM made up of the younger and older generations.
Then, reclaiming memory from younger generation space (including Eden and Survivor areas) is called Minor GC.
Major GC is cleaning up the old days.
Full GC is cleaning up the entire heap space-including the younger generation and the older generation.
12. Garbage collection algorithm
Commentary: the basic question, the blogger dared to guess, should be asking what garbage collection algorithms are. The interviewer should not have the patience to listen to you memorize the algorithm concept one by one!
Answer:
Tag-clearing algorithm, tag finishing algorithm, replication algorithm, generational collection algorithm
13. Garbage collector G1
Commentary: the above topic is asked in more depth. JVM can be configured with different recyclers. Such as Serial, Parallel and CMS garbage collectors. Take the Serial Collector (Serial Collector) as an example. In the younger generation, it is a collector that uses the tag-copy algorithm. In the old days, the mark-clean-clean algorithm was used.
In addition, there are many questions about the G1 recycler, the author did not clearly describe the point of the G1 recycler, just answer the concept!
If I ask, I'll just give you the scene and ask you what kind of recycler you should use. Ask the recycler directly, it will be easier!
Common parameters:
-XX:+UseSerialGC: using serial collectors in the new and old generations
-XX:+UseParNewGC: using parallel collectors in the new generation
/ / check it yourself, it's too much!
Answer:
G1 GC is one of the new features of Jdk7, and the Jdk7+ version can independently configure G1 as a JVM GC option. G1 divides the whole heap into equal-sized chunks (each block is called a region). Each block of memory is contiguous, and each block also acts as Eden, Survivor, and Old, but they are not fixed, which makes memory use more flexible. As shown in the following figure
When performing garbage collection, the collection thread executes concurrently with the application thread in the marking phase. After the mark is over, G1 will know which blocks are basically garbage, and there are very few surviving objects. G1 will start with these blocks first, because a lot of free space can be quickly released from these blocks, which is why G1 is named Garbage-First.
14. ElasticSearch and Hbase have been used in the project. Do you have an in-depth understanding of their tuning skills?
Commentary: a new graduate ElasticSearch and Hbase, generally only demo level, understand the basic use of CRUD! Generally do not go to in-depth understanding of tuning techniques! If you have answered this question in depth, it is to dig a hole for yourself! Because of this question, the answer is too wide!
Answer: I don't know much about it!
15. The concrete realization of Spring RestTemplate
Commentary: the blogger of this question is a little confused! If I were to ask, I would first ask if there are so many clients accessing the Rest service, why Spring RestTemplate? And then come to the principle. This suddenly appeared a concrete realization, I am a little confused!
Answer:
In fact, RestTemplate and sl4fj this kind of facade framework is very similar, the essence is to add a vest in the Http network request, itself does not have its own implementation.
The bottom layer can support http access of a variety of httpclient, the upper layer is ClientHttpRequestFactory API class, and the bottom layer is as follows:
RestTemplate then encapsulates the underlying details of assembling, sending HTTP messages, and parsing the response.
It's OK to answer this question. Do you want to draw a reference diagram of the relationship between classes? It's so unrealistic!
16. Describe the whole process of a Http request from the web page to the backend
Commentary: basic questions, feeling belongs to common sense questions! I will!
Answer:
Use DNS for domain name resolution-> initiate 3-way handshake of TCP-> initiate http request after establishing TCP connection-> server responds to http request, browser gets html code-- > browser parses html code and requests resources in html code (such as js, css, picture, etc.)-> browser renders the page to the user
17. Common methods, interface classes and thread pool mechanisms for multithreading
Commentary: basic questions, basic thread knowledge, more or less will answer! But this question, I feel the scope is a little big! Maybe the author didn't make it clear!
Answer:
Common methods:
Start,run,sleep,wait,notify,notifyAll,join,isAlive,currentThread,interrupt
Common interface classes:
Runnable 、 Callable 、 Future 、 FutureTask
The mechanism of thread pool:
In object-oriented programming, creating and destroying objects is time-consuming, because creating an object requires access to memory resources or other resources. So one way to improve the efficiency of service programs is to minimize the number of times objects are created and destroyed, so pooling technology has emerged.
A simple thread pool consists of the following four components:
Thread Pool Manager (ThreadPoolManager): used to create and manage thread pools
Worker threads (WorkThread): threads in a thread pool
Task interface (Task): the interface that each task must implement for worker threads to schedule task execution
Task queue: used to store unprocessed tasks. Provide a buffer mechanism
18. Summing up my Java foundation is still good, but some mainstream framework source code is still in use, so we need to continue to look at the source code.
Comment: frankly, I don't see any problems that reflect that the mainstream framework is still in use.
19. Deadlock
Answer:
Deadlock refers to the phenomenon that two or more processes wait for each other because of competing for resources in the process of execution. if there is no external force, they will not be able to push forward, if the system resources are sufficient, the resource requests of the process can be met, the possibility of deadlock is very low, otherwise it will fall into a deadlock because of limited resources.
The main reasons for deadlocks are:
(1) because of insufficient system resources.
(2) the order in which the process runs and advances is not appropriate.
(3) improper allocation of resources.
20, their own research on relatively new technology, talk about the results!
Commentary: well, highlight your potential, everyone is free to play!
21. What do you want to ask? I just asked about the situation in the company, this free play!
Commentary: just ask about the content of the job, don't ask about benefits or overtime! Don't ask this kind of question on the technical side!
The above is all the contents of the article "Analysis of 21 interview questions in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.