您好,欢迎访问一九零五行业门户网

Java日常开发的21个坑,你踩过几个?

前言最近看了极客时间的《java业务开发常见错误100例》,再结合平时踩的一些代码坑,写写总结,希望对大家有帮助,感谢阅读~
1. 六类典型空指针问题包装类型的空指针问题级联调用的空指针问题equals方法左边的空指针问题concurrenthashmap 这样的容器不支持 key 和 value 为 null。集合,数组直接获取元素对象直接获取属性1.1包装类型的空指针问题public class nullpointtest { public static void main(string[] args) throws interruptedexception { system.out.println(testinteger(null)); } private static integer testinteger(integer i) { return i + 1; //包装类型,传参可能为null,直接计算,则会导致空指针问题 }}
1.2 级联调用的空指针问题public class nullpointtest { public static void main(string[] args) { //fruitservice.getappleservice() 可能为空,会导致空指针问题 fruitservice.getappleservice().getweight().equals("ok"); }}
1.3 equals方法左边的空指针问题public class nullpointtest { public static void main(string[] args) { string s = null; if (s.equals("666")) { //s可能为空,会导致空指针问题 system.out.println("公众号:捡田螺的小男孩,666"); } }}
1.4 concurrenthashmap 这样的容器不支持 key,value 为 null。public class nullpointtest { public static void main(string[] args) { map map = new concurrenthashmap<>(); string key = null; string value = null; map.put(key, value); }}
1.5 集合,数组直接获取元素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.300000000000000040.19999999999999996401.499999999999941.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.30000000000000001665334536937734810635447502136230468750.1999999999999999555910790149937383830547332763671875401.499999999999968025576890795491635799407958984375001.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:truec==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<integer> 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"; } }}
输出结果:
测试switch12testswitch结果是: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()); }}
运行结果:
1
arrays.aslist源码如下:
public static <t> list<t> 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的时候,对原始数组的修改会影响到我们获得的那个listpublic 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<string> list = new arraylist<string>(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) { //生产环境别用它 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当前执行第几:pool-1-thread-3当前执行第几:pool-1-thread-4发生异常啦当前执行第几:pool-1-thread-6当前执行第几:pool-1-thread-7当前执行第几:pool-1-thread-8当前执行第几:pool-1-thread-9当前执行第几:pool-1-thread-10
可以发现,如果是使用submit方法提交到线程池的异步任务,异常会被吞掉的,所以在日常发现中,如果会有可预见的异常,可以采取这几种方案处理:
1.在任务代码try/catch捕获异常2.通过future对象的get方法接收抛出的异常,再处理3.为工作者线程设置uncaughtexceptionhandler,在uncaughtexception方法中处理异常4.重写threadpoolexecutor的afterexecute方法,处理传递的异常引用11.5 finally重新抛出的异常也要注意啦public void wrong() { try { log.info("try"); //异常丢失 throw new runtimeexception("try"); } finally { log.info("finally"); throw new runtimeexception("finally"); }}
一个方法是不会出现两个异常的呢,所以finally的异常会把try的「异常覆盖」。正确的使用方式应该是,finally 代码块「负责自己的异常捕获和处理」。
public void right() { try { log.info("try"); throw new runtimeexception("try"); } finally { log.info("finally"); try { throw new runtimeexception("finally"); } catch (exception ex) { log.error("finally", ex); } }}
12.json序列化,long类型被转成integer类型!public class jsontest { public static void main(string[] args) { long idvalue = 3000l; map<string, object> data = new hashmap<>(2); data.put("id", idvalue); data.put("name", "捡田螺的小男孩"); assert.assertequals(idvalue, (long) data.get("id")); string jsonstring = json.tojsonstring(data); // 反序列化时long被转为了integer map map = json.parseobject(jsonstring, map.class); object idobj = map.get("id"); system.out.println("反序列化的类型是否为integer:"+(idobj instanceof integer)); assert.assertequals(idvalue, (long) idobj); }}
「运行结果:」
exception in thread "main" 反序列化的类型是否为integer:truejava.lang.classcastexception: java.lang.integer cannot be cast to java.lang.long at object.jsontest.main(jsontest.java:24)
❝「注意啦」,序列化为json串后,josn串是没有long类型呢。而且反序列化回来如果也是object接收,数字小于interger最大值的话,给转成integer啦!
❞13. 使用executors声明线程池,newfixedthreadpool的oom问题executorservice executor = executors.newfixedthreadpool(10); for (int i = 0; i < integer.max_value; i++) { executor.execute(() -> { try { thread.sleep(10000); } catch (interruptedexception e) { //do nothing } }); }
「ide指定jvm参数:-xmx8m -xms8m :」
运行结果:
我们看下源码,其实newfixedthreadpool使用的是无界队列!
public static executorservice newfixedthreadpool(int nthreads) { return new threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>());}public class linkedblockingqueue<e> extends abstractqueue<e> implements blockingqueue<e>, java.io.serializable { ... /** * creates a {@code linkedblockingqueue} with a capacity of * {@link integer#max_value}. */ public linkedblockingqueue() { this(integer.max_value); }...}
❝newfixedthreadpool线程池的核心线程数是固定的,它使用了近乎于无界的linkedblockingqueue阻塞队列。当核心线程用完后,任务会入队到阻塞队列,如果任务执行的时间比较长,没有释放,会导致越来越多的任务堆积到阻塞队列,最后导致机器的内存使用不停的飙升,造成jvm oom。
❞14. 直接大文件或者一次性从数据库读取太多数据到内存,可能导致oom问题如果一次性把大文件或者数据库太多数据达到内存,是会导致oom的。所以,为什么查询db数据库,一般都建议分批。
读取文件的话,一般问文件不会太大,才使用files.readalllines()。为什么呢?因为它是直接把文件都读到内存的,预估下不会oom才使用这个吧,可以看下它的源码:
public static list<string> readalllines(path path, charset cs) throws ioexception { try (bufferedreader reader = newbufferedreader(path, cs)) { list<string> result = new arraylist<>(); for (;;) { string line = reader.readline(); if (line == null) break; result.add(line); } return result; }}
如果是太大的文件,可以使用files.line()按需读取,当时读取文件这些,一般是使用完需要「关闭资源流」的哈
15. 先查询,再更新/删除的并发一致性问题再日常开发中,这种代码实现经常可见:先查询是否有剩余可用的票,再去更新票余量。
if(selectisavailable(ticketid){ 1、deleteticketbyid(ticketid) 2、给现金增加操作 }else{ return “没有可用现金券” }
如果是并发执行,很可能有问题的,应该利用数据库的更新/删除的原子性,正解如下:
if(deleteavailableticketbyid(ticketid) == 1){ 1、给现金增加操作 }else{ return “没有可用现金券” }
16. 数据库使用utf-8存储, 插入表情异常的坑低版本的mysql支持的utf8编码,最大字符长度为 3 字节,但是呢,存储表情需要4个字节,因此如果用utf8存储表情的话,会报sqlexception: incorrect string value: '\xf0\x9f\x98\x84' for column,所以一般用utf8mb4编码去存储表情。
17. 事务未生效的坑日常业务开发中,我们经常跟事务打交道,「事务失效」主要有以下几个场景:
底层数据库引擎不支持事务在非public修饰的方法使用rollbackfor属性设置错误本类方法直接调用异常被try...catch吃了,导致事务失效。其中,最容易踩的坑就是后面两个,「注解的事务方法给本类方法直接调用」,伪代码如下:
public class transactiontest{ public void a(){ //插入一条数据 //调用方法b (本地的类调用,事务失效了) b(); } @transactional public void b(){ //插入数据 }}
如果异常被catch住,「那事务也是会失效呢」~,伪代码如下:
@transactionalpublic void method(){ try{ //插入一条数据 inserta(); //更改一条数据 updateb(); }catch(exception e){ logger.error("异常被捕获了,那你的事务就失效咯",e); }}
18. 当反射遇到方法重载的坑/** * 反射demo * @author 捡田螺的小男孩 */public class reflectiontest { private void score(int score) { system.out.println("int grade =" + score); } private void score(integer score) { system.out.println("integer grade =" + score); } public static void main(string[] args) throws exception { reflectiontest reflectiontest = new reflectiontest(); reflectiontest.score(100); reflectiontest.score(integer.valueof(100)); reflectiontest.getclass().getdeclaredmethod("score", integer.type).invoke(reflectiontest, integer.valueof("60")); reflectiontest.getclass().getdeclaredmethod("score", integer.class).invoke(reflectiontest, integer.valueof("60")); }}
运行结果:
int grade =100integer grade =100int grade =60integer grade =60
如果「不通过反射」,传入integer.valueof(100),走的是integer重载。但是呢,反射不是根据入参类型确定方法重载的,而是「以反射获取方法时传入的方法名称和参数类型来确定」的
getclass().getdeclaredmethod("score", integer.class)getclass().getdeclaredmethod("score", integer.type)
19. mysql 时间 timestamp的坑有更新语句的时候,timestamp可能会自动更新为当前时间,看个demo
create table `t` ( `a` int(11) default null, `b` timestamp not null, `c` timestamp not null default current_timestamp on update current_timestamp) engine=innodb default charset=utf8
我们可以发现 「c列」 是有current_timestamp on update current_timestamp,所以c列会随着记录更新而「更新为当前时间」。但是b列也会随着有记录更新为而「更新为当前时间」。
可以使用datetime代替它,需要更新为当前时间,就把now()赋值进来,或者修改mysql的这个参数explicit_defaults_for_timestamp。
20. mysql8数据库的时区坑之前我们对mysql数据库进行升级,新版本为8.0.12。但是升级完之后,发现now()函数,获取到的时间比北京时间晚8小时,原来是因为mysql8默认为美国那边的时间,需要指定下时区
jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai
参考与感谢[1]java业务开发常见错误100例: https://time.geekbang.org/column/article/220230
以上就是java日常开发的21个坑,你踩过几个?的详细内容。
其它类似信息

推荐信息