判断变量s是否为空 s == null 和null == s 的区别

来源:百度知道 编辑:UC知道 时间:2024/06/06 17:49:32
定义一个字符串变量string s = "a"; 在程序中写判断语句:
if(s == null){...} 和 if (null == s){...} 有区别么?还是个人习惯的问题?

为了安全,好的编程习惯用null == s这种

因为有可能犯低级错误,把双等号==写为=

null = s 是编译不过去的,编译器报错,你就知道自己写错了

效果上来看,两者一样

一样的