在对对象判断为空:Object != null 和 null != Object的区别?
Java中对null进行判断放在前后没有什么区别,只是为了代码规范,为了避免写代码时书写错误。下面面两个测试Demo都没有报错。null放在前面是为了避免少写一个"=","null="书写会报错,防止笔误写成"=null"时检查不出来。
@Test
public void testDemo1(){
String str = null;
if (str==null){
return;
}
}
@Test
public void testDemo2(){
String str = null;
if (null==str){
return;
}
}