简而言之就是 : null的出现是声明了对象,判断没有给对象开辟空间; “”空是判断给对象开辟了空间但没有值的情况,String类型同时具备这两种情况,所以都要进行判断,已达到万无一失。
//判断字符串为空串
public boolean checkStringIsNull(String str){
if(str ==null || “”.equals(str)){
return true;
}
if (!StringUtil.isNullString(carrierCode3)) {
carrierCode3 = carrierCode3.trim();
}
public static boolean isEmpty(Collection coll) {undefined
return (coll == null || coll.isEmpty());
}
if (str != null && !"".equals(str))
if (str != null && str.length() != 0)
package referenceType3; import com.sun.deploy.util.StringUtils; public class TT { static String str2; public static void main(String[] args) { String str1 = ""; String str3 = "abc"; System.out.println("str1长度为0"); new TT().extracted(str1); System.out.println(""); System.out.println("str2为null"); new TT().extracted(str2); System.out.println(""); System.out.println("str3为abc"); new TT().extracted(str3); } private void extracted(String str) { //if (str != null && !"".equals(str)) { if (str != null && str.length() != 0) { System.out.println("true"); System.out.println("str = " + str); } else { System.out.println("false"); System.out.println("str = " + str); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)