Summary of 2018 Java interview questions

Copyright statement: This article is a reprinted article, the original text:  click to open the link

The following interview questions are a summary of recent popular Java interview questions, for reference only!


1. Introduce yourself and introduce a recent project

Briefly introduce yourself, name, place of origin, how many years you have worked since graduation, and which companies you have worked for. Just take it with a word. Then I will introduce a recent project, including the overall architecture design, involving front-end and back-end frameworks, caching, middleware, and databases.


Second, the String feature. Difference between StringBuffer and StringBuilder

String a = "str"; String b = new String("str"); ask a == b ,  is the value of a.equals(b) true or false? Here is the difference between == and equals: 

    ==: Compare reference types to compare whether the address values ​​are the same

    equals: By default, comparing reference types also compares whether the address values ​​are the same, while the String class overrides the equals() method to compare whether the contents are the same.

   String a = "str"; , "str" ​​is stored in the string constant pool of the method area. And String b = new String("str");, new String() is stored in the heap, pointing to the "str" ​​of the constant pool. When using ==, a points to the address of the string constant pool, and b points to the address stored in the new String() heap. So necessarily false. And String's equals is the comparison content. so is true.

    About String to know that it is an immutable class modified by final. Frequent modifications are best to use  StringBuffer or StringBuilder. The difference between the two is that StringBuilder is efficient and thread-unsafe. StringBuffer is thread safe.


3. Principles, differences and underlying data structures of ArrayList and LinkedList

     the difference:

  • ArrayList is a data structure based on dynamic arrays, and LinkedList is a data structure based on linked lists. 
  • For random access get and set, ArrayList feels better than LinkedList, because LinkedList has to move the pointer. 
  • For the add and delete operations add and remove, the LinedList is more dominant, because the ArrayList needs to move the data. 
     Knowing this is not enough, it is best to look at the source code. Learn why these differences exist. Taking the third point as an example, why does adding and deleting ArrayList have an advantage? If you look at the source code, you will understand. The underlying structure of ArrayList is an array. First of all, the array is immutable. When adding a new array, check whether the size of the array is satisfied. If it does not meet the need for expansion, you need to create a new array, then copy the original array to the new array, and then empty the original array. Array added. When deleting, the ArrayList will move the elements of the latter part up one position in turn (that is, copy). Other methods are no longer described, and you need to familiarize yourself with the source code first. Sometimes it is asked how to ensure the thread safety of ArrayList. Use Collections.synchronizedList() or use  Vector instead. The principle is to add the synchronize keyword before the method to achieve locking.

Fourth, HashMap, HashTable, ConcurrentHashMap principle, source code, data structure, thread safety

    HashMap is an array plus linked list structure. Find the array index by the hashcode of the key. Then traverse the linked list.

    The difference between HashTable and HashMap is thread-safe, and the key value cannot be null.

   ConcurrentHashMap is also thread-safe, and the structure can be understood as a split array + HashTable. The efficiency is higher than HashTable, because HashTable locks the entire Map, and ConcurrentHashMap adds locks to each split array.


Five, Lock and synchronize implementation principles and differences. Briefly describe optimistic locking and pessimistic locking. Distributed lock implementation

    Synchronized is managed for JVM execution, and lock is the code written by java to control the lock. If the synchronized method block throws an exception when it is locked, the JVM will automatically release the lock, and will not cause thread deadlock if the lock is not released due to an exception. But Lock can't enjoy the automatic function brought by JVM. When an exception occurs, the lock must be finally released, otherwise it will cause deadlock. In the case where resource competition is not very fierce, and there is occasional synchronization, synchronized is very suitable. The reason is that the compiler usually optimizes the synchronization as much as possible. 

    Distributed lock implementation: based on database implementation, based on cache (Redis, memcached, tair) implementation, based on Zookeeper implementation.


Six, SpringMVC process. Common annotations. Spring's IOC and AOP

SpringMVC process:

  1. The user sends a request to the front controller DispatcherServlet.
  2. The DispatcherServlet receives the request and calls the HandlerMapping handler mapper.
  3. The processor mapper finds a specific processor (which can be searched according to xml configuration and annotations), generates a processor object and a processor interceptor (if any) and returns it to DispatcherServlet.
  4. DispatcherServlet calls HandlerAdapter handler adapter.
  5. HandlerAdapter is adapted to call a specific handler (Controller, also called a back-end controller).
  6. Controller执行完成返回ModelAndView。
  7. HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
  8. DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
  9. ViewReslover解析后返回具体View。
  10. DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。
  11. DispatcherServlet响应用户。


七、微服务架构Spring Boot ,Spring Cloud

1.微服务的优势

2.Spring Cloud 五大神兽:(要知道实现原理,Eureka 是重点)

  • 服务发现——Netflix Eureka
  • 客服端负载均衡——Netflix Ribbon
  • 断路器——Netflix Hystrix
  • 服务网关——Netflix Zuul
  • 分布式配置——Spring Cloud Config

八、多线程与高并发

    多线程要了解线程常用方法。线程池的实现方式。如何避免死锁。生命周期。并发包常用类原理和应用场景:计数器CountdownLatch,栅栏CyclicBarrier,信号量semaphore,交换。


九、事务相关

项目中事务处理方案。

4种事务特性:
  • 原子性 (atomicity):强调事务的不可分割. 
  • 一致性 (consistency):事务的执行的前后数据的完整性保持一致. 
  • 隔离性 (isolation):一个事务执行的过程中,不应该受到其他事务的干扰 
  • 持久性(durability) :事务一旦结束,数据就持久到数据库

5种隔离级别:

  • DEFAULT 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别
  • 未提交读(read uncommited) :脏读,不可重复读,虚读都有可能发生 
  • 已提交读 (read commited):避免脏读。但是不可重复读和虚读有可能发生 
  • 可重复读 (repeatable read) :避免脏读和不可重复读.但是虚读有可能发生
  • 串行化的 (serializable) :避免以上所有读问题

7种传播行为:

    保证同一个事务中 

  • PROPAGATION_REQUIRED 支持当前事务,如果不存在 就新建一个(默认) 
  • PROPAGATION_SUPPORTS 支持当前事务,如果不存在,就不使用事务 
  • PROPAGATION_MANDATORY 支持当前事务,如果不存在,抛出异常 
   保证没有在同一个事务中 
  • PROPAGATION_REQUIRES_NEW 如果有事务存在,挂起当前事务,创建一个新的事务 
  • PROPAGATION_NOT_SUPPORTED 以非事务方式运行,如果有事务存在,挂起当前事务 
  • PROPAGATION_NEVER 以非事务方式运行,如果有事务存在,抛出异常 
  • PROPAGATION_NESTED 如果当前事务存在,则嵌套事务执行

分布式事务处理方案(XA,TCC等,也可以了解一下阿里最近推出的 GTS 推荐博客:https://blog.csdn.net/mine_song/article/details/64118963


十、常用那些设计模式,说出原理,已经框架中用到哪些。单例的几种写法

    23种设计模式至少要了解Spring框架用到的一些设计模式。bean单例 beanfactory工厂 AOP动态代理等。

    单例有饿汉式,懒汉式,静态内部类等方式。这些方式并非真正安全,可以用反射拿到,用枚举可以避免。


十一、常用中间件RabbitMQ,kafka等,原理、区别、优缺点

中间件的应用场景。

RabbitMQ是一个AMQP实现,传统的messaging queue系统实现,基于Erlang。老牌MQ产品了。AMQP协议更多用在企业系统内,对数据一致性、稳定性和可靠性要求很高的场景,对性能和吞吐量还在其次。

Kafka是linkedin开源的MQ系统,主要特点是基于Pull的模式来处理消息消费,追求高吞吐量,一开始的目的就是用于日志收集和传输,0.8开始支持复制,不支持事务,适合产生大量数据的互联网服务的数据收集业务。


十二、缓存:Redis原理,数据结构,集群几种方式。和memcache区别


十三、HTTP协议,RPC协议,Socket协议


十四、Nginx实现负载均衡的几种方式

    轮询,随机,哈希,加权轮询,加权随机,最小连接数等。


十五、java1.8新特性


十六、关于jvm调优



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324678895&siteId=291194637