public class NullPointTest {
public static void main(String[] args) {
int [] array=null;
List list = null;
System.out.println(array[0]); //空指针异常
System.out.println(list.get(0)); //空指针一场
}
}
1.6 对象直接获取属性
============
public class NullPointTest {
public static void main(String[] args) {
User user=null;
System.out.println(user.getAge()); //空指针异常
}
}
2. 日期YYYY格式设置的坑
================
日常开发,经常需要对日期格式化,但是呢,年份设置为YYYY大写的时候,是有坑的哦。
反例:
Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.DECEMBER, 31);
Date testDate = calendar.getTime();
SimpleDateFormat dtf = new SimpleDateFormat(“YYYY-MM-dd”);
System.out.println("2019-12-31 转 YYYY-MM-dd 格式后 " + dtf.format(testDate));
运行结果:
2019-12-31 转 YYYY-MM-dd 格式后 2020-12-31
解析:
===
为什么明明是2019年12月31号,就转了一下格式,就变成了2020年12月31号了?因为YYYY是基于周来计算年的,它指向当天所在周属于的年份,一周从周日开始算起,周六结束,只要本周跨年,那么这一周就算下一年的了。正确姿势是使用yyyy格式。
正例:
Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.DECEMBER, 31);
Date testDate = calendar.getTime();
SimpleDateFormat dtf = new SimpleDateFormat(“yyyy-MM-dd”);
System.out.println("2019-12-31 转 yyyy-MM-dd 格式后 " + dtf.format(testDate));
3.金额数值计算精度的坑
============
看下这个浮点数计算的例子吧:
public class DoubleTest {
public static void main(String[] args) {
System.out.println(0.1+0.2);
System.out.println(1.0-0.8);
System.out.println(4.015*100);
System.out.println(123.3/100);
double amount1 = 3.15;
double amount2 = 2.10;
if (amount1 - amount2 == 1.05){
System.out.println(“OK”);
}
}
}
运行结果:
0.30000000000000004
0.19999999999999996
401.49999999999994
1.2329999999999999
可以发现,结算结果跟我们预期不一致,其实是因为计算机是以二进制存储数值的,对于浮点数也是。对于计算机而言,0.1无法精确表达,这就是为什么浮点数会导致精确度缺失的。因此, 金额计算,一般都是用BigDecimal 类型
对于以上例子,我们改为BigDecimal,再看看运行效果:
System.out.println(new BigDecimal(0.1).add(new BigDecimal(0.2)));
System.out.println(new BigDecimal(1.0).subtract(new BigDecimal(0.8)));
System.out.println(new BigDecimal(4.015).multiply(new BigDecimal(100)));
System.out.println(new BigDecimal(123.3).divide(new BigDecimal(100)));
运行结果:
0.3000000000000000166533453693773481063544750213623046875
0.1999999999999999555910790149937383830547332763671875
401.49999999999996802557689079549163579940795898437500
1.232999999999999971578290569595992565155029296875
发现结果还是不对, 其实 ,使用 BigDecimal 表示和计算浮点数,必须使用 字符串的构造方法来初始化 BigDecimal,正例如下:
public class DoubleTest {
public static void main(String[] args) {
System.out.println(new BigDecimal(“0.1”).add(new BigDecimal(“0.2”)));
System.out.println(new BigDecimal(“1.0”).subtract(new BigDecimal(“0.8”)));
System.out.println(new BigDecimal(“4.015”).multiply(new BigDecimal(“100”)));
System.out.println(new BigDecimal(“123.3”).divide(new BigDecimal(“100”)));
}
}
在进行金额计算,使用BigDecimal的时候,我们还需要 注意BigDecimal的几位小数点,还有它的八种舍入模式哈 。
4. FileReader默认编码导致乱码问题
========================
看下这个例子:
public class FileReaderTest {
public static void main(String[] args) throws IOException {
Files.deleteIfExists(Paths.get(“jay.txt”));
Files.write(Paths.get(“jay.txt”), “你好,捡田螺的小男孩”.getBytes(Charset.forName(“GBK”)));
System.out.println(“系统默认编码:”+Charset.defaultCharset());
char[] chars = new char[10];
String content = “”;
try (FileReader fileReader = new FileReader(“jay.txt”)) {
int count;
while ((count = fileReader.read(chars)) != -1) {
content += new String(chars, 0, count);
}
}
System.out.println(content);
}
}
运行结果:
系统默认编码:UTF-8
���,�����ݵ�С�к�
从运行结果,可以知道,系统默认编码是utf8,demo中读取出来,出现乱码了。为什么呢?
FileReader 是以当 前机器的默认字符集 来读取文件的,如果希望指定字符集的话,需要直接使用 InputStreamReader 和 FileInputStream。
正例如下:
public class FileReaderTest {
public static void main(String[] args) throws IOException {
Files.deleteIfExists(Paths.get(“jay.txt”));
Files.write(Paths.get(“jay.txt”), “你好,捡田螺的小男孩”.getBytes(Charset.forName(“GBK”)));
System.out.println(“系统默认编码:”+Charset.defaultCharset());
char[] chars = new char[10];
String content = “”;
try (FileInputStream fileInputStream = new FileInputStream(“jay.txt”);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName(“GBK”))) {
int count;
while ((count = inputStreamReader.read(chars)) != -1) {
content += new String(chars, 0, count);
}
}
System.out.println(content);
}
}
5. Integer缓存的坑
===============
public class IntegerTest {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println(“a==b:”+ (a == b));
Integer c = 128;
Integer d = 128;
System.out.println(“c==d:”+ (c == d));
}
}
运行结果:
a==b:true
c==d:false
为什么Integer值如果是128就不相等了呢? 编译器会把 Integer a = 127 转换为 Integer.valueOf(127)。 我们看下源码。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以发现,i在一定范围内,是会返回缓存的。
默认情况下呢,这个缓存区间就是[-128, 127],所以我们业务日常开发中,如果涉及Integer值的比较,需要注意这个坑哈。还有呢,设置 JVM 参数加上 -XX:AutoBoxCacheMax=1000,是可以调整这个区间参数的,大家可以自己试一下哈
6. static静态变量依赖spring实例化变量,可能导致初始化出错
=====================================
之前看到过类似的代码。静态变量依赖于spring容器的bean。
private static SmsService smsService = SpringContextUtils.getBean(SmsService.class);
这个静态的smsService有可能获取不到的,因为类加载顺序不是确定的,正确的写法可以这样,如下:
private static SmsService smsService =null;
//使用到的时候采取获取
public static SmsService getSmsService(){
if(smsService==null){
smsService = SpringContextUtils.getBean(SmsService.class);
}
return smsService;
}
7. 使用ThreadLocal,线程重用导致信息错乱的坑
==============================
使用ThreadLocal缓存信息,有可能出现信息错乱的情况。看下下面这个例子吧。
private static final ThreadLocal currentUser = ThreadLocal.withInitial(() -> null);
@GetMapping(“wrong”)
public Map wrong(@RequestParam(“userId”) Integer userId) {
//设置用户信息之前先查询一次ThreadLocal中的用户信息
String before = Thread.currentThread().getName() + “:” + currentUser.get();
//设置用户信息到ThreadLocal
currentUser.set(userId);
//设置用户信息之后再查询一次ThreadLocal中的用户信息
String after = Thread.currentThread().getName() + “:” + currentUser.get();
//汇总输出两次查询结果
Map result = new HashMap();
result.put(“before”, before);
result.put(“after”, after);
return result;
}
按理说,每次获取的before应该都是null,但是呢,程序运行在 Tomcat 中,执行程序的线程是 Tomcat 的工作线程,而 Tomcat 的工作线程是基于线程池的。
线程池会重用固定的几个线程,一旦线程重用,那么很可能首次从 ThreadLocal 获取的值是之前其他用户的请求遗留的值。这时,ThreadLocal 中的用户信息就是其他用户的信息。
把tomcat的工作线程设置为1
server.tomcat.max-threads=1
用户1,请求过来,会有以下结果,符合预期:
用户2请求过来,会有以下结果, 不符合预期 :
因此,使用类似 ThreadLocal 工具来存放一些数据时,需要特别注意在代码运行完后,显式地去清空设置的数据,正例如下:
@GetMapping(“right”)
public Map right(@RequestParam(“userId”) Integer userId) {
String before = Thread.currentThread().getName() + “:” + currentUser.get();
currentUser.set(userId);
try {
String after = Thread.currentThread().getName() + “:” + currentUser.get();
Map result = new HashMap();
result.put(“before”, before);
result.put(“after”, after);
return result;
} finally {
//在finally代码块中删除ThreadLocal中的数据,确保数据不串
currentUser.remove();
}
}
8. 疏忽switch的return和break
=========================
这一点严格来说,应该不算坑,但是呢,大家写代码的时候,有些朋友容易疏忽了。直接看例子吧
/*
-
关注公众号:
-
捡田螺的小男孩
*/
public class SwitchTest {
public static void main(String[] args) throws InterruptedException {
System.out.println(“testSwitch结果是:”+testSwitch(“1”));
}
private static String testSwitch(String key) {
switch (key) {
case “1”:
System.out.println(“1”);
case “2”:
System.out.println(2);
return “2”;
case “3”:
System.out.println(“3”);
default:
System.out.println(“返回默认值”);
return “4”;
}
}
}
输出结果:
测试switch
1
2
testSwitch结果是:2
switch 是会 沿着case一直往下匹配的,直到遇到return或者break。 所以,在写代码的时候留意一下,是不是你要的结果。
9. Arrays.asList的几个坑
=====================
9.1 基本类型不能作为 Arrays.asList方法的参数,否则会被当做一个参数。
===========================================
public class ArrayAsListTest {
public static void main(String[] args) {
int[] array = {1, 2, 3};
List list = Arrays.asList(array);
System.out.println(list.size());
}
}
运行结果:
Arrays.asList源码如下:
public static List asList(T… a) {
return new ArrayList<>(a);
}
9.2 Arrays.asList 返回的 List 不支持增删操作。
===================================
public class ArrayAsListTest {
public static void main(String[] args) {
String[] array = {“1”, “2”, “3”};
List list = Arrays.asList(array);
list.add(“5”);
System.out.println(list.size());
}
}
运行结果:
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at object.ArrayAsListTest.main(ArrayAsListTest.java:11)
Arrays.asList 返回的 List 并不是我们期望的 java.util.ArrayList,而是 Arrays 的内部类 ArrayList。内部类的ArrayList没有实现add方法,而是父类的add方法的实现,是会抛出异常的呢。
9.3 使用Arrays.asLis的时候,对原始数组的修改会影响到我们获得的那个List
=============================================
public class ArrayAsListTest {
public static void main(String[] args) {
String[] arr = {“1”, “2”, “3”};
List list = Arrays.asList(arr);
arr[1] = “4”;
System.out.println(“原始数组”+Arrays.toString(arr));
System.out.println(“list数组” + list);
}
}
运行结果:
原始数组[1, 4, 3]
list数组[1, 4, 3]
从运行结果可以看到,原数组改变,Arrays.asList转化来的list也跟着改变啦,大家使用的时候要注意一下哦,可以用new ArrayList(Arrays.asList(arr))包一下的。
10. ArrayList.toArray() 强转的坑
=============================
public class ArrayListTest {
public static void main(String[] args) {
List list = new ArrayList(1);
list.add(“公众号:捡田螺的小男孩”);
String[] array21 = (String[])list.toArray();//类型转换异常
}
}
因为返回的是Object类型,Object类型数组强转String数组,会发生ClassCastException。解决方案是,使用toArray()重载方法toArray(T[] a)
String[] array1 = list.toArray(new String[0]);//可以正常运行
11. 异常使用的几个坑
=============
11.1 不要弄丢了你的堆栈异常信息
==================
public void wrong1(){
try {
readFile();
} catch (IOException e) {
//没有把异常e取出来,原始异常信息丢失
throw new RuntimeException(“系统忙请稍后再试”);
}
}
public void wrong2(){
try {
readFile();
} catch (IOException e) {
//只保留了异常消息,栈没有记录啦
log.error(“文件读取错误, {}”, e.getMessage());
throw new RuntimeException(“系统忙请稍后再试”);
}
}
正确的打印方式,应该酱紫
public void right(){
try {
readFile();
} catch (IOException e) {
//把整个IO异常都记录下来,而不是只打印消息
log.error(“文件读取错误”, e);
throw new RuntimeException(“系统忙请稍后再试”);
}
}
11.2 不要把异常定义为静态变量
=================
public void testStaticExeceptionOne{
try {
exceptionOne();
} catch (Exception ex) {
log.error(“exception one error”, ex);
}
try {
exceptionTwo();
} catch (Exception ex) {
log.error(“exception two error”, ex);
}
}
private void exceptionOne() {
//这里有问题
throw Exceptions.ONEORTWO;
}
private void exceptionTwo() {
//这里有问题
throw Exceptions.ONEORTWO;
}
exceptionTwo抛出的异常,很可能是 exceptionOne的异常哦。正确使用方法,应该是new 一个出来。
private void exceptionTwo() {
throw new BusinessException(“业务异常”, 0001);
}
11.3 生产环境不要使用e.printStackTrace();
=================================
public void wrong(){
try {
readFile();
} catch (IOException e) {
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
//生产环境别用它
e.printStackTrace();
}
}
因为它占用太多内存,造成锁死,并且,日志交错混合,也不易读。正确使用如下:
log.error(“异常日志正常打印方式”,e);
11.4 线程池提交过程中,出现异常怎么办?
======================
public class ThreadExceptionTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
IntStream.rangeClosed(1, 10).forEach(i -> executorService.submit(()-> {
if (i == 5) {
System.out.println(“发生异常啦”);
throw new RuntimeException(“error”);
}
System.out.println(“当前执行第几:” + Thread.currentThread().getName() );
}
));
executorService.shutdown();
}
}
运行结果:
当前执行第几:pool-1-thread-1
当前执行第几:pool-1-thread-2