哪个是使用非空条件的最佳方式,是直接条件还是使用实用方法?
Which is the best way to use not null condition,Whether direct condition or using utility methods?
我想在很多地方检查非空条件,那么哪种方式性能最好..?
1.Whether if(object !=null){ ... }
2.Whether if(Util.isNotNull(object)){...}
第一种方法是继续
的更好方法
if(object !=null){ ... }
因为它将使用 short circuit evaluation.
干脆
if(obj == null) throw new IllegalArgumentException();
else{
// Do your computations
....
}
有不同的实用程序 类 可用于检查空值和空值,具体取决于您要检查的对象。
对于字符串,您有 org.apache.commons.lang.StringUtils.isBlank
,它执行以下操作:
检查字符串是否为空格、空 ("") 或 null。
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
对于集合,您有 org.apache.commons.collections.CollectionUtils.isEmpty
检查您的集合是否不为 null 且不为空
我想在很多地方检查非空条件,那么哪种方式性能最好..?
1.Whether if(object !=null){ ... }
2.Whether if(Util.isNotNull(object)){...}
第一种方法是继续
的更好方法if(object !=null){ ... }
因为它将使用 short circuit evaluation.
干脆
if(obj == null) throw new IllegalArgumentException();
else{
// Do your computations
....
}
有不同的实用程序 类 可用于检查空值和空值,具体取决于您要检查的对象。
对于字符串,您有 org.apache.commons.lang.StringUtils.isBlank
,它执行以下操作:
检查字符串是否为空格、空 ("") 或 null。
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
对于集合,您有 org.apache.commons.collections.CollectionUtils.isEmpty
检查您的集合是否不为 null 且不为空