1. What problem is solved by the Strategy pattern in Java?

Strategy pattern allows you to introduce a new algorithm or new strategy without changing the code which uses that algorithm. For example, the Collections.sort() method which sorts the list of the object uses the Strategy pattern to compare objects. Since every object uses a different comparison strategy you can compare various objects differently without changing the sort method.

阅读全文

Java 面试题汇编 (三)

1. What is method overloading in OOP or Java?

Method overloading in Java is an object-oriented programming concept that allows a programmer to declare two methods of the same name but with different method signatures, like a change in the argument list or a change in the type of argument. Original method :

public void  show(String message){
      System.out.println(message);
}

Overloaded method: number of arguments is different:

阅读全文

Java 面试题汇编 (二)

1. Difference between String, StringBuffer, and StringBuilder in Java?

String is immutable while both StringBuffer and StringBuilder are mutable, which means any change e.g. converting String to upper case or trimming white space will produce another instance rather than changing the same instance. In later two, StringBuffer is synchronized while StringBuilder is not, in fact, it’s a ditto replacement of StringBuffer added in Java 1.5.

阅读全文

Java 面试题汇编 (一)

1. map里,hashmap无序的,想要有序的该怎么办?
HashMap

  • Map存储过程:计算key的hashCode值,确定位置;如果位置上有元素,用equals判断是否一致,如果一致,不存,如果不一致,遍历下一个节点继续判断equals。注:添加是添加到链表的尾部。 加载因子(扩容条件):默认0.75,空间存储75%后扩容。
  • 扩容:将原来的元素重新计算位置再存入;计算需要时间,因此不可以频繁扩容,扩容是成倍的。
  • HashMap根据键可以很快找到他的值,因此具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。
  • HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。
  • HashMap的键只允许一条为null,值可以多个为null

阅读全文

Spring Bean 的生命周期

生命周期的主要流程

Bean 的生命周期概括起来就是 4 个阶段:

  1. 实例化(Instantiation)
  2. 属性赋值(Populate)
  3. 初始化(Initialization)
  4. 销毁(Destruction)

阅读全文

Java 问题排查

运行参数

关于java启动参数配置,大家可以参阅:官方文档

查看JVM参数

使用命令 jps -l 查看所有正在运行的Java程序,同时显示启动类类名,获取到PID

4706 org.apache.catalina.startup.Bootstrap
5023 sun.tools.jps.Jps

阅读全文

作者的图片

闲散工程师笔记

一名不断探索、坚持学习的闲散工程师

云原生.盐酒猿

CHINA🇨🇳