2021 java technical interview questions finishing

1. Servlet execution process

The client sends an http request, and the web server forwards the request to the servlet container. The servlet container parses the url and finds the corresponding servlet according to web.xml, and passes the request and response objects to the found servlet. The servlet can know who it is based on the request. For the sent request, request information and other information, when the servlet processes the business logic, it will put the information into the response and respond to the client.

2. The execution process of springMVC

springMVC is a hierarchical control framework with dispatchservlet as the core. First, the client sends a request to the web server to parse the request url and match the mapping url of the dispatchservlet. If it matches, it will put the request into the dispatchservlet. The dispatchservlet will find the corresponding handel according to the mapping mapping configuration, and then give the processing power to find The handel, handel encapsulates the code for processing business logic. When the handel is processed, it will return a logical view modelandview to dispatchservlet. At this time, the modelandview is a logical view and not a formal view, so dispatchservlet will resolve the modelandview through the viewresource view resource. Then put the parsed parameters in the view, return to the client and display.

3. Given a txt file, how to get the number of occurrences of a string

1

2

3

4

5

6

7

8

9

10

File file = new File("E://test.txt");

InputStream is = new FileInputStream(file);

byte b[] = new byte[1024];

int a = is.read(b);

String str[] = new String(b,0,a).split("");

int count = 0;

for(int i = 0;i<str.length;i++){

if("a".equals(str[i]))count++;

}

System.out.println(count);

4. Java design pattern ideas (single-column model, factory model, strategy model, a total of 23 design patterns)

a) Singleton mode: The core of the singleton mode only needs a new instance object mode, such as database connection, online number of people, etc. The online number of people counted on some websites is realized through the singleton mode, and a timer is stored in In the database or memory, when someone logs in, take it out and add it back again, and when someone logs out, take it out and subtract it again and put it back again, but when two people log in at the same time, the counter is taken out at the same time, plus one, and put back at the same time. Go back, in this case the data will be wrong, so you need a global variable object for everyone to use, only need to create an instance object, this is the application of the singleton mode, and the singleton mode saves resources, because it controls the instance object The number, and is conducive to gc recovery.

b) Strategy mode: It is to extract the public methods of several classes into a new class, so as to make the extension easier, to ensure the portability of the code and strong maintainability. For example, there is a requirement to write duck objects. There are three methods for ducks to call, fly, and shape. If each duck class writes these three methods, there will be code redundancy. At this time, we can call the ducks, fly , The three methods of shape are extracted and placed in the duck parent class, so that each duck inherits the duck parent class, and rewrites these three methods, so that the encapsulated code is highly portable. When users put forward new requirements, such as Ducks can swim, so it is very simple for us oo programmers. We only need to add a swimming method to the duck parent class, and let the swimming duck rewrite the swimming method.

c) Factory mode: The simple factory mode is mainly to provide a unified reference to the instance object, and obtain the reference of the instance object through the factory mode interface. For example, for a login function, the backend has three classes, the controller class, the interface class, and the implementation class that implements the interface. When the client sends a request, when the request is passed to the controller class, the controller obtains the reference object of the interface, and the implementation class that implements the interface encapsulates the login business logic code. When you need to add a registration requirement, you only need to add a registration method to the interface class, implement the method in the implementation class, and the controller can obtain the reference object of the interface without changing the original code. This approach is extensible Strong.

5. Bubble sort, binary search

a) bubbling

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public static void mp(int a[]) {

int swap = 0;

for (int i = 0; i < a.length; i++) {

for (int j = i; j < a.length; j++) {

if (a[j] > a[i]) {

swap = a[i];

a[i] = a[j];

a[j] = swap;

}

}

}

System.out.println(Arrays.toString(a));

}

b) Binary search

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public static int ef(int a[], int tag) {

int first = 0;

int end = a.length;

for (int i = 0; i < a.length; i++) {

int middle = (first + end) / 2;

if (tag == a[middle]) {

return middle;

}

if (tag > a[middle]) {

first = middle + 1;

}

if (tag < a[middle]) {

end = middle - 1;

}

}

return 0;

}

6-8, understanding of ajax

a) Ajax为异步请求,即局部刷新技术,在传统的页面中,用户需要点击按钮或者事件触发请求,到刷新页面,而异步技术为不需要点击即可触发事件,这样使得用户体验感增强,比如商城购物车的异步加载,当你点击商品时无需请求后台而直接动态修改参数。

9、父类与子类之间的调用顺序(打印结果)

a) 父类静态代码块

b) 子类静态代码块

c) 父类构造方法

d) 子类构造方法

e) 子类普通方法

f) 重写父类的方法,则打印重写后的方法

10、内部类与外部类的调用

a) 内部类可以直接调用外部类包括private的成员变量,使用外部类引用的this.关键字调用即可

b) 而外部类调用内部类需要建立内部类对象

11、多线程

a)一个进程是一个独立的运行环境,可以看做是一个程序,而线程可以看做是进程的一个任务,比如QQ是一个进程,而一个QQ窗口是一个线程。

b)在多线程程序中,多线程并发可以提高程序的效率,cpu不会因为某个线程等待资源而进入空闲状态,它会把资源让给其他的线程。

c)用户线程就是我们开发程序是创建的线程,而守护线程为系统线程,如JVM虚拟中的GC

d)线程的优先级别:每一个线程都有优先级别,有限级别高的可以先获取CPU资源使该线程从就绪状态转为运行状态。也可以自定义线程的有限级别

e)死锁:至少两个以上线程争取两个以上cpu资源,避免死锁就避免使用嵌套锁,只需要在他们需要同步的地方加锁和避免无限等待

12、AOP与IOC的概念(即spring的核心)

a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度。而spring的核心是IOC控制反转和AOP面向切面编程。IOC控制反转主要强调的是程序之间的关系是由容器控制的,容器控制对象,控制了对外部资源的获取。而反转即为,在传统的编程中都是由我们创建对象获取依赖对象,而在IOC中是容器帮我们创建对象并注入依赖对象,正是容器帮我们查找和注入对象,对象是被获取,所以叫反转。

b) AOP:面向切面编程,主要是管理系统层的业务,比如日志,权限,事物等。AOP是将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封装为一个可重用的模块,这个模块被命名为切面(aspect),切面将那些与业务逻辑无关,却被业务模块共同调用的逻辑提取并封装起来,减少了系统中的重复代码,降低了模块间的耦合度,同时提高了系统的可维护性。

13、hibernate的核心思想

a) Hibernate的核心思想是ROM对象关系映射机制。它是将表与表之间的操作映射成对象与对象之间的操作。也就是从数据库中提取的信息会自动按照你设置的映射要求封装成特定的对象。所以hibernate就是通过将数据表实体类的映射,使得对对象的修改对应数据行的修改。

14、Struts1与Struts2的区别

15、最优删除谋字符串的某个字符

16-17、Arraylist与linkedlist的区别

a) 都是实现list接口的列表,arraylist是基于数组的数据结构,linkedlist是基于链表的数据结构,当获取特定元素时,ArrayList效率比较快,它通过数组下标即可获取,而linkedlist则需要移动指针。当存储元素与删除元素时linkedlist效率较快,只需要将指针移动指定位置增加或者删除即可,而arraylist需要移动数据。

18、数据库优化

a) 选择合适的字段,比如邮箱字段可以设为char(6),尽量把字段设置为notnull,这样查询的时候数据库就不需要比较null值

b) 使用关联查询( left join on)查询代替子查询

c) 使用union联合查询手动创建临时表

d) 开启事物,当数据库执行多条语句出现错误时,事物会回滚,可以维护数据库的完整性

e) 使用外键,事物可以维护数据的完整性但是它却不能保证数据的关联性,使用外键可以保证数据的关联性

f) 使用索引,索引是提高数据库性能的常用方法,它可以令数据库服务器以比没有索引快的多的速度检索特定的行,特别是对于max,min,order by查询时,效果更明显

g) 优化的查询语句,绝大多数情况下,使用索引可以提高查询的速度,但如果sql语句使用不恰当的话,索引无法发挥它的特性。

19、Tomcat服务器优化(内存,并发连接数,缓存)

a) 内存优化:主要是对Tomcat启动参数进行优化,我们可以在Tomcat启动脚本中修改它的最大内存数等等。

b) 线程数优化:Tomcat的并发连接参数,主要在Tomcat配置文件中server.xml中配置,比如修改最小空闲连接线程数,用于提高系统处理性能等等。

c) 优化缓存:打开压缩功能,修改参数,比如压缩的输出内容大小默认为2KB,可以适当的修改。

20、HTTP协议

a) 常用的请求方法有get、post

b) Get与post的区别:传送数据,get携带参数与访问地址传送,用户可以看见,这的话信息会不安全,导致信息泄露。而post则将字段与对应值封装在实体中传送,这个过程用户是不可见的。Get传递参数有限制,而post无限制。

21、TCP/UDP协议

22、Java集合类框架的基本接口有哪些

a) Collection集合接口,List、set实现Collection接口,arraylist、linkedlist,vector实现list接口,stack继承vector,Map接口,hashtable、hashmap实现map接口

23、类加载的过程

a) 遇到一个新的类时,首先会到方法区去找class文件,如果没有找到就会去硬盘中找class文件,找到后会返回,将class文件加载到方法区中,在类加载的时候,静态成员变量会被分配到方法区的静态区域,非静态成员变量分配到非静态区域,然后开始给静态成员变量初始化,赋默认值,赋完默认值后,会根据静态成员变量书写的位置赋显示值,然后执行静态代码。当所有的静态代码执行完,类加载才算完成。

24、对象的创建

a) 遇到一个新类时,会进行类的加载,定位到class文件

b) 对所有静态成员变量初始化,静态代码块也会执行,而且只在类加载的时候执行一次

c) New 对象时,jvm会在堆中分配一个足够大的存储空间

d) 存储空间清空,为所有的变量赋默认值,所有的对象引用赋值为null

e) 根据书写的位置给字段一些初始化操作

f) 调用构造器方法(没有继承)

25、jvm的优化

a) 设置参数,设置jvm的最大内存数

b) 垃圾回收器的选择

26、高并发处理

a) 了解一点高并发性问题,比如一W人抢一张票时,如何保证票在没买走的情况下所有人都能看见这张票,显然是不能用同步机制,因为synchronize是锁同步一次只能一个人进行。这时候可以用到锁机制,采用乐观锁可以解决这个问题。乐观锁的简单意思是在不锁定表的情况下,利用业务的控制来解决并发问题,这样即保证数据的可读性,又保证保存数据的排他性,保证性能的同时解决了并发带来的脏读数据问题。

27、事物的理解

a) 事物具有原子性,一致性,持久性,隔离性

b) 原子性:是指在一个事物中,要么全部执行成功,要么全部失败回滚。

c) 一致性:事物执行之前和执行之后都处于一致性状态

d) 持久性:事物多数据的操作是永久性

e) 隔离性:当一个事物正在对数据进行操作时,另一个事物不可以对数据进行操作,也就是多个并发事物之间相互隔离。

28、Struts工作流程

a) 客户端发出一个请求到servlet容器

b) 请求经过一些列过滤被filterdispatcher调用,filterdispatch通过actionMapper去找相对应的action。

c) Actionmapper找到对应的action返回给filterdispatch,dispatch把处理权交给actionproxy

d) Actionproxy通过配置文件找到对应的action类

e) Actionproxy创建一个actionIinvocation的实例处理业务逻辑

f) 一旦action处理完毕,actioninvocation负责根据stuts.xml的配置找到对应的返回结果。返回结果通常是jsp页面。

Guess you like

Origin blog.csdn.net/weixin_47385625/article/details/113249646