在java中检查字符串/对象的空值? [英] Checking null value for a String/Object in java?
本文介绍了在java中检查字符串/对象的空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么好处
String a = null;
if(null != a)
结束
if(a !=null)
我尝试了两种语句,它们都运行良好.有什么建议为什么我应该选择第一个?
I tried both statement and they worked fine. Any suggestions why should I go with the first one?
推荐答案
两者都是一样的,但是如果你在一个布尔值上检查 ==
:
Both are the same, however if you are checking for ==
on a boolean value:
if(a == true)
对比
if(true == a)
后者会更好,因为只输入 =
而不是 ==
会导致排版错误:
The latter would be better due to the tendency of typographical error by typing only =
instead of ==
:
if(a = true) //still compilable but not what you intended
if(true = a) //cannot be compiled, hence you know you typed it wrongly
这篇关于在java中检查字符串/对象的空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文