当前位置:网站首页>Explain the interview questions by holding chestnuts (interview, review and study)
Explain the interview questions by holding chestnuts (interview, review and study)
2022-06-10 19:18:00 【Hua Weiyun】
Preface :
This article mainly introduces some common questions in our interview , And the key points we need to pay attention to in our learning process . If the boss is currently looking for a job , You can look at the problem first , Think before you see the answer , Students who have just learned here can also use the topic to consolidate .
If there is something wrong in the article or needs to be improved , Please don't hesitate to give advice .
Xiao Wei would like to thank you for your support 
Personal home page : Xiao Wei wants to learn from the guys
🧑 Personal profile : Hello everyone , I'm Xiao Wei , A man who wants to make progress with everyoneIf the boss is preparing for an interview , You can use the brush artifact I'm also using Here's the brush artifact
Welcome to : Here is CSDN, Where I sum up my knowledge , Welcome to my blog , My dear boss
@[toc]
🧁Java Eight basic data types


Java The eight basic data types are integer :short,int,long; Byte type :byte; floating-point :float,double; Character :char; Boolean type :boolean.
Three basic characteristics of object orientation
The three basic features of object-oriented are encapsulation , Inheritance and polymorphism .
encapsulation
encapsulation : Hide the properties and implementation details of some objects , Access to data can only be made through public interfaces , Control the access level of property reading and modification in the program , Will abstract the resulting data and behavior ( Or function ) Combination , Form an organic whole , This method provides different levels of protection for internal data , What prevents unintended changes or errors in unimportant parts of the program is the private part of the object .
Inherit
Inherit : A method that allows an object of one type to obtain the properties of an object of another type , Inheritance is that the child class inherits the characteristics and behaviors of the parent class , Methods that make objects of subclasses have parent classes , Or the subclass inherits the method from the parent class Is a subclass that has the same behavior as the parent class Use extends keyword Implementation inheritance Inheritance is that subclasses automatically share the data and methods of the parent class , Is a relationship between classes , It improves the reusability and extensibility of the software .
polymorphic
polymorphic : Polymorphism is to use the parent class when declaring, and the specific subclass when implementing or calling , Without modifying the code, you can change the specific code bound when the program runs , Let the program choose multiple running states, which is polymorphism For the same behavior , Different subclass objects have different manifestations , There are three conditions for the existence of polymorphism : Inherit , Method rewriting , A parent class references an object that points to a child class .
🥧 The difference between abstract class and interface
First, explain what an abstract class is :
Extract the common features between classes , You can form abstract classes .
Abstract classes cannot instantiate objects directly , But you can use polymorphism That is, the reference of the parent class points to the object of the child class , Abstract class as parent class .
What is an interface :
Interfaces can be seen as special abstract classes , Of course, interfaces cannot instantiate and create objects , Polymorphism can also be used when using , The reference of the parent class points to the object of the child class , The parent class here is the interface .
difference :
- Abstract classes embody inheritance (extends), The interface embodies the implementation relationship (implements), A class can implement multiple interfaces , A class can only inherit one abstract class .
- Abstract classes can define , Common method , Static methods , Abstract method , Provided to subclasses to use , The methods in the interface are abstract , Members in an interface have fixed modifiers .
- There can be construction methods in abstract classes , And there can be no constructor in the interface
- The access type of an abstract method in an abstract class can be public,protected, But an abstract method in an interface can only be public Type of , And the default is public abstract type .

== and equals() Differences in methods
1.== The sign is an operator , and equals() Is the method ;
2.== You can compare basic types of data , You can also compare data of reference types , Judge whether two variables or instances point to the same memory space .equals() Only data of reference type can be compared , Used to check the equality of objects , For example, if the double equals the sign and equals() Used to compare objects , When two objects have the same reference address , Double equals returns TRUE, and equals() Method can return TRUE or FALSE, It mainly depends on the implementation of method rewriting .
3.== Compare memory addresses , equals() Is a comparison of the contents of a string .
tip :
String str = "xiaowei"; First look in memory to see if there is "xiaowei" This object , If there is , let str Point to that "hello"String s = new String("xiaowei");
Like any other object , Each call produces an object , As long as they call .
String str = “xiaowei”; If there is no memory "xiaowei", Just create a new object to save "xiaowei". String str=new String (“xiaowei”) It doesn't matter if it's already in memory "xiaowei" This object , Create a new object to save "xiaowei".
🧁Mybatis in #{} and ${} The difference between
First of all SQL Inject
SQL Injection is a code injection technique , Used to attack data-driven applications , Malicious SQL Statement is inserted into the executed entity field , The attacker can use form information or URL Enter some strange SQL fragment ( for example “or ‘1’=‘1’” Such a statement ), It is possible to invade applications with insufficient parameter verification .
So the difference between the two methods is reflected :
1.#{ Variable name } Can be precompiled 、 Type matching and other operations ,#{ Variable name } Will translate into jdbc The type of .# To a great extent sql Inject ;
2.$ { Variable name } No data type matching , Direct replacement . The way cannot be the way sql Inject .$ Method is generally used for incoming database objects , For example, pass in the table name ;
3.# Will automatically add double quotes ,$ No double quotes .
These two symbols are in mybatis The most direct difference is :# It is equivalent to adding single quotation marks to the data ,$ It's equivalent to displaying data directly ( Discuss only string types ).
Take a chestnut :
# Treat the passed in parameters as strings , That is, it will precompile ,
select * from user where name = #{name}, For example, I pass a xiaowei, So it's justselect * from user where name = 'xiaowei';$ The values passed in will not be precompiled ,
select * from user where name=${name}, For example, I pass a xiaowei, So it's justselect * from user where name = xiaowei;# The advantage of is that it can prevent sql Inject , and $ No way. . such as : The user performs a login operation , backstage sql Verify the style of :
select * from user where username=#{name} and password = #{pwd}, If the user name from the front desk is “xiaoweihaoshuai”, The password is “1 or 1=1”, use # The way will not appear sql Inject , And if you change to $ The way ,sql The sentence becomesselect * from user where username=wang and password = 1 or 1=1. In this way, it forms sql Inject .
@Autowired and @Resource The difference between
@Autowired yes spring The implementation provided by the framework depends on ⼊ Annotations , The main ⽀ Hold on to set⽅ Law ,field, Constructor bean notes ⼊, notes ⼊⽅ The expression is to find by type bean, namely byType Type of , If there are multiple identical ⼀ Type of bean, makes ⽤@Qualifier To specify the note ⼊ Which one? beanName Of bean.
And JDK Of @Resource The difference between :@Resource Is based on bean Name , namely beanName Type of , Come from spring Of IOC Container lookup bean notes ⼊ Of ,⽽@Autowried It's based on type byType Search for bean notes ⼊ Of .
And JDK Of @Inject The difference between :@Inject It's also based on type bean notes ⼊ Of , If you need to specify a name beanName, Can be combined to make ⽤@Named annotation ,⽽@Autowired Is a combination of @Qualifier Annotation to specify the name beanName.
1.@Autowired、@Inject It is matched by type by default ,@Resource Match by name . If in spring-boot-data term ⽬ in ⾃ dynamic ⽣ Yes redisTemplate Of bean, It needs to pass byName Let's make a note of ⼊ Of . If you need to note ⼊ The default , You need to make ⽤@Resource Let's make a note of ⼊,⽽ No @Autowired.
2. about @Autowire and @Inject, If the same ⼀ Multiple types exist bean example , You need to specify a note ⼊ Of beanName.
3.@Autowired and @Qualifier⼀ Start to make ⽤,@Inject and @Name⼀ Start to make ⽤.
Deadlock , The difference between optimistic lock and pessimistic lock
First, let's introduce what deadlock is :
Two or more threads , In the competition for resources, a deadlock occurs when you wait for the other party's execution to continue , Finally, these threads are trapped in infinite waiting .
Three handshakes and four waves
Because I have written relevant articles in detail before , So attach a link directly to the friends. Hey, hey 🤩🤩 Three handshakes and four waves
Pessimistic locking
Pessimistic locking : Always assume the worst , Every time I go to get the data, I think others will modify it , So every time I get the data, I lock it , That way, people who want to take this data will block it , Until it gets the lock .
Pessimistic lock is also called mutually exclusive synchronous lock , It is to ensure the correctness of the results , Every time we get the data , Will lock it , So when other threads also access , It's going to be blocked , This prevents other threads from accessing the data , So as to ensure the security of data .
Java We often use Synchronized And RenntrantLock It's all pessimistic .
This locking mechanism is used in many places of the database , For example, line locks. , Table locks , Read the lock , Write locks, etc. .
Take a chestnut :
select * from user where id='1' for updateThis article sql Statement locked user All items in the table meet the search criteria (id=‘1’) The record of . This data is locked by the current transaction , Other transactions must wait until this transaction is committed , That is, other threads enter the blocking state . This ensures that the current data will not be modified by other transactions .( Premise is id Fields must be primary keys or unique indexes , Or lock the watch , Something will happen .)
For the pessimistic lock , Only one transaction can occupy the resource , Other transactions are suspended waiting for the transaction holding the resource to commit and release the resource ,CPU These threads that cannot get resources will be suspended , Suspended threads also consume CPU Resources for , Especially in Gao Jingfa's request .
Once the thread commits the transaction , Then the lock will be released , At this time, the suspended thread will start competing for resources , Then the competing threads will be CPU Back to running state , Continue operation .
In the process of high concurrency , Using pessimistic locks will cause a large number of threads to be suspended and restored , This will It consumes a lot of resources , So the performance of pessimistic lock is very poor .

Optimism lock
Optimistic lock is a kind of thought , Always assume the best , Every time I go to get the data, I think other people won't modify it , So it won't lock , Only when updating, we will judge whether others have updated this data during this period . If a thread is modifying data , No other threads interfere , Then the modification will be executed normally ; If the data has been modified by other threads , In order to ensure the correctness of the data , Will give up or report an error .
Optimistic locking is a mechanism that does not block the concurrency of other threads , It won't be implemented with database locks , Its design does not block other threads , Therefore, it will not cause frequent thread suspension and recovery , This can improve concurrency , So some people call it non blocking lock .
Pessimistic locks or optimistic locks are not intended to replace each other , There is no difference between good and bad , Just for different scenarios . Less competition for resources ( Less thread conflict ) The situation of , Optimistic locks are better .

sketch Spring Of ioc
Without using Spring Before development , We need to finish a project new Many objects , These objects are created between us , And these objects may also have some dependencies , For example, two objects need to be used cooperatively , This results in a high degree of coupling between two or more objects , Then reduce the coupling between objects , It needs to be used Spring Of ioc 了 .
Spring Of ioc, That is, the reversal of control (Inversion of Control), It's not a new technology , It is Spring A design idea of . stay Spring There is a special container in to create and manage these objects , And inject other objects that the object depends on into the object , This container is generally called ioc Containers .
All the classes we create will be in Spring Register... In container , tell Spring What is it? , What do you need , then spring When the system is running properly , Offer you what you want , At the same time, give you to other things that need you . Creation of all classes 、 Destroyed by Spring To control , That is to say, it is no longer the object that references it that controls the object's life cycle , It is Spring. For a specific object , It used to control other objects , Now it's all the objects that are Spring control , So this is called inversion of control .
DI: Dependency injection (Dependency Injection)Spring After the container loads the configuration file , Objects that create classes through reflection , And assign a value to the property ;Spring There are three ways for containers to implement attribute injection through reflection :
- set Methods to inject
- Constructor Injection
- Interface injection ( Not commonly used )

SpringMVC workflow

- The request sent by the front end is sent by the front end controller DispatcherServlet Intercept
- The front controller calls the processor mapper HandlerMapping Request for URL To analyze , After parsing, return the call to the front-end controller
- The front-end controller calls the processor adapter to handle the call chain
- The processor adapter completes the processor design through the adapter design pattern based on reflection ( controller ) The call of handles user requests
- The processor adapter encapsulates the view and data information returned by the controller into ModelAndView Respond to the front-end controller
- The front-end controller calls the view parser ViewResolver Yes ModelAndView To analyze , Will parse the result ( Resources and views ) Respond to the front-end controller
- The front-end controller invokes the view view Component renders the data , The results will be rendered ( Static view ) Respond to the front-end controller
- The front-end controller responds to user requests

Component description
DispatcherServlet: Front controller , Also known as the central controller , It's the control center for the entire request response , The call of a component is uniformly scheduled by it .
HandlerMapping: Processor mapper , It's based on the user's access to URL Map to the corresponding back-end processor Handler. That is, it knows the back-end processor that processes user requests , But it doesn't execute the back-end processor , It's telling the processor to the CPU .
HandlerAdapter: Processor adapter , It calls methods in the back-end processor , Return to logical view ModelAndView object .
ViewResolver: view resolver , take ModelAndView The logical view resolves to a specific view ( Such as JSP).
Handler: Back end processor , Process specific requests from users , That's what we wrote Controller class .
The article ends here , If the big guys like this kind of article , It will be updated later , It's not easy to make , Please also ask the big guys to give more support
If the article is not written properly or needs to be improved , Please correct me more
Finally, Xiao Wei thanks you again for your support

边栏推荐
- 阵列信号处理仿真之四——Z变换分析阵列多项式
- 数据治理经典6大痛点?这本书教你解决
- Some summary about YUV format
- Chapter 1 SQL operators
- 2022.05.26(LC_1143_最长公共子序列)
- 《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
- 源码分析及实践测试OpenFeign负载均衡
- mysql(17-课后练习题)
- Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
- Adobe Premiere Basics - introduction, configuration, shortcut keys, creating projects, creating sequences (I)
猜你喜欢

Rk1126 adds a new module
![[vulnhub range] janchow: 1.0.1](/img/b5/e3f0d213ee87cd60802ee3db79d10f.png)
[vulnhub range] janchow: 1.0.1

Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe

数据治理经典6大痛点?这本书教你解决

Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)

2022.05.26(LC_1143_最长公共子序列)

Use of uiautomator2 automated test tool

Introduction to ad18 device library import

基于ssm在线订餐系统设计与实现.rar(项目源码)

AEC: analysis of echo generation causes and echo cancellation principle
随机推荐
Openssl1.1.1 compilation error can't locate win32/console pm in @INC
Chapter 161 SQL function year
WordPress 6.0 “Arturo阿图罗” 发布
2022.05.28(LC_5_最长回文子串)
Openssl1.1.1 vs2013 compilation tutorial
Opencv does not rely on any third-party database for face detection
AEC: analysis of echo generation causes and echo cancellation principle
Design and development of hospital reservation registration platform based on JSP Zip (thesis + project source code)
Detailed explanation of Lora module wireless transceiver communication technology
Screen output of DB2 stored procedure, output parameters, and return result set
OPENCV 检测人脸 不依赖于任何第三方库
SQL 函数
SQL statement to view the basic table structure and constraint fields, primary codes and foreign codes in the table (simple and effective)
mysql(17-觸發器)
New trends and prospects of data center planning and design under the background of "double carbon"
Win32 child window parent window window owner
【数据库语言SPL】写着简单跑得又快的数据库语言 SPL
Chapter II data type (I)
抢唱玩法升级,正版音乐高潮片段、实时打分能力等你集成~
c(指针02)


