至少感觉 isNotEmpty 这个方法的名字不太好比如这么写代码的时候 if (!isNotEmpty(XXX))总得动脑筋想想什么情况下走if分支,什么时候走else分支不能让大多数程序员秒懂的代码不是好代码
/** * Returns {@code true} if the given string is null or is the empty string. * * <p>Consider normalizing your string references with {@link #nullToEmpty}. * If you do, you can use {@link String#isEmpty()} instead of this * method, and you won't need special null-safe forms of methods like {@link * String#toUpperCase} either. Or, if you'd like to normalize "in the other * direction," converting empty strings to {@code null}, you can use {@link * #emptyToNull}. * * @param string a string reference to check * @return {@code true} if the string is null or is the empty string */ public static boolean isNullOrEmpty(@Nullable String string) { return string == null || string.length() == 0; // string.isEmpty() in Java 6 }http://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/base/Strings.java
boolean isNull = true;
if(str != null && !"".equals(str.trim()) && !"null".equals(str)){
isNull = false;
}
return isNull;
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
应该自己在外面trim了 传进来
可以测试下,处理时间,就知道了,顺便监控下,JVM的内存用量。
public static boolean isNotEmpty(String string) {
return string != null && string != "";
}
public static isNotEmpty(String str) {
return str != null && !str.isEmpty();
}
如果是要判断某个字符串是否等于一个常量字符串"hello".equals(str);
如果是要返回一个字符串的话
str != null ? str : "";
第一个不管从代码设计还是效率上来说,都是比较不错的。但是注释不敢恭维,注释的意思和方法命名、代码实现明显相反。ps:中式英语用得不错
第二个问题很多。
1、方法的命名,很难理解。从方法上看,这个方法肯定是返回boolean类型更好。
2、这个方法从命名上看,应该只需要判断Null.但是在实现上使用了trim。trim()的效率是很低的。并且如果看过jdk trim方法实现的应该知道。java对字符串的拆分、trim等都会造成大量的垃圾内存,这个方法在频发调用时效率会更低。吐槽一下:
1、虽然是很简单的util方法,但是写util是很考验人的。每个人都要对自己每一行代码负责。这是一种态度。
2、对于注释和方法命名,用一句很流行的话:“取个名字很难”。方法命名应该让别人从你的方法名上就知道你要干嘛。对注释,要么你命名好方法,就啥都别写,要写就要很准确的表达。否则误人子弟
硬说要方法的恶化如果要我写
我可能会写成 str.trim().isEmpty();
{
return s == null || s.empty();
}
"" : empty
" " : whitespace
一般都是
if(str!=null && !"".equals(str))
{
........
}
if (!isNotEmpty(XXX))总得动脑筋想想什么情况下走if分支,什么时候走else分支不能让大多数程序员秒懂的代码不是好代码
* Returns {@code true} if the given string is null or is the empty string.
*
* <p>Consider normalizing your string references with {@link #nullToEmpty}.
* If you do, you can use {@link String#isEmpty()} instead of this
* method, and you won't need special null-safe forms of methods like {@link
* String#toUpperCase} either. Or, if you'd like to normalize "in the other
* direction," converting empty strings to {@code null}, you can use {@link
* #emptyToNull}.
*
* @param string a string reference to check
* @return {@code true} if the string is null or is the empty string
*/
public static boolean isNullOrEmpty(@Nullable String string) {
return string == null || string.length() == 0; // string.isEmpty() in Java 6
}http://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/base/Strings.java
return parmValue == null;
}
public static boolean isEmpty(String parmValue) {
return (parmValue == null) || ("".equals(parmValue));
}
这样写也可以你说呢?