You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NewYoung edited this page Aug 12, 2020
·
1 revision
都是过来人,谁没被坑过呢
说说我工作这些年被java坑过的地方.
== 和 equals重要性
我想大家应该都碰到过用==号来判断两个对象是否相等
项目开发中,肯定又不少人这么使用过。 有时候单元测试成功通过,线上确跑出问题。
String,Long,Integer java对这三种(至少三种,还有其它的我没具体了解) 类型的对象会有缓存,通常我们定义一个 String a = “abc”; Integer b=1; Long c =2L; 以上三对象都会被缓存到常量池中,但是String有点不同, 它会缓存所有这样定义的对象。 Integer和Long会又缓存区间,(-128到127)也就是 Long a=1000L;Long b=1000L; System.out.pritnln(a==b);这时候输出的是false
在特殊的情况下我们会调整java缓存来阻止更多的对象被new。 -XX:AutoBoxCacheMax=65535(不一定在所有jvm上生效) 这时候 Long a=65535;Long b=65535; 他们两是绝对相等的。