Pre-test code
package com.study.string; import cn.hutool.core.util.StrUtil; import org.springframework.util.StringUtils; /** * @ClassName StringTest * @Description How to judge null or "" in Java * @Author Jiangnan Cui * @Date 2023/3/21 21:54 * @Version 1.0 */ public class StringTest { public static void main(String[] args) { String string = "test"; String nullString = null; String emptyString = ""; // 0. Judge null + judge "": intuitive and convenient, but extremely inefficient if(nullString == null || nullString == ""){// I am null! System.out.println("I am null!"); } if(emptyString == null || emptyString == ""){// I am ! System.out.println("I am !"); } // 1. Judging null + judging "": Before judging "", ensure that the string is not null, otherwise a null pointer exception will be thrown when calling the String method if(nullString == null || nullString.length() == 0){// I am null! System.out.println("I am null!"); } if(emptyString == null || emptyString.length() == 0){// I am ! System.out.println("I am !"); } // 2. Judging null + judging "": Before judging "", ensure that the string is not null, and put the non-null string in front, otherwise a null pointer exception will occur if(nullString == null || "".equals(nullString)){//I am null! System.out.println("I am null!"); } if(emptyString == null || "".equals(emptyString)){// I am ! System.out.println("I am !"); } // 3. Judging null + judging "": Before judging "", ensure that the string is not null, otherwise a null pointer exception will be thrown when calling the String method if(nullString == null || nullString.isEmpty()){// I am null! System.out.println("I am null!"); } if(emptyString == null || emptyString.isEmpty()){// I am ! System.out.println("I am !"); } /** * The underlying source code: * private final char value[]; * public boolean isEmpty() { * return value. length == 0; * } * The bottom layer still calls the length method of the array, provided that it is not null */ // 4. Judge null + judge "" if (StringUtils.isEmpty(nullString)) {// I am null! System.out.println("I am null!"); } if (StringUtils.isEmpty(emptyString)) {// I am ! System.out.println("I am !"); } /** * The underlying source code: * @Deprecated * public static boolean isEmpty(@Nullable Object str) { * return str == null || "".equals(str); * } * The bottom layer is to judge whether the string is null or "" */ // 5. The isEmpty method in Hutool: judge null + judge "" boolean b = StrUtil.isEmpty(string);// b = false System.out.println("b = " + b); boolean isNull = StrUtil.isEmpty(nullString);// isNull = true System.out.println("isNull = " + isNull); boolean isEmpty = StrUtil.isEmpty(emptyString);// isEmpty = true System.out.println("isEmpty = " + isEmpty); /** * The underlying source code: * public static boolean isEmpty(CharSequence str) { * return str == null || str. length() == 0; * } * The bottom layer is to judge whether the string is null or "" */ // 6. The isBlank method in Hutool: judge null + judge "" b = StrUtil.isBlank(string);// b = false System.out.println("b = " + b); isNull = StrUtil.isBlank(nullString);// isNull = true System.out.println("isNull = " + isNull); isEmpty = StrUtil.isBlank(emptyString);// isEmpty = true System.out.println("isEmpty = " + isEmpty); /** * The underlying source code: * public static boolean isBlank(CharSequence str) { * int length; * if (str != null & amp; & amp; (length = str. length()) != 0) { * for(int i = 0; i < length; + + i) { * if (!CharUtil.isBlankChar(str.charAt(i))) { * return false; * } * } * return true; * } else { * return true; * } * } */ /** * It is recommended to use the isEmpty or isBlank method of the Hutool tool class: */ System.out.println(StrUtil.isEmpty("test"));// false System.out.println(StrUtil.isEmpty(""));//true System.out.println(StrUtil.isEmpty(null));//true System.out.println(StrUtil.isBlank("test"));// false System.out.println(StrUtil.isBlank(""));//true System.out.println(StrUtil.isBlank(null));//true System.out.println(StrUtil.isNotEmpty("test"));//true System.out.println(StrUtil.isNotEmpty(""));// false System.out.println(StrUtil.isNotEmpty(null));// false System.out.println(StrUtil.isNotBlank("test"));//true System.out.println(StrUtil.isNotBlank(""));// false System.out.println(StrUtil.isNotBlank(null));// false } }
Summary
In practical applications, it is recommended to use the methods under the StrUtil class in the Hutool tool class (isEmpty, isNotEmpty, isBlank, isNotBlank) to judge null or “”. The corresponding source code is as follows:
public static boolean isEmptyIfStr(Object obj) { if (null == obj) { return true; } else if (obj instanceof CharSequence) { return 0 == ((CharSequence) obj). length(); } else { return false; } } public static boolean isNotEmpty(CharSequence str) { return !isEmpty(str); } public static boolean isBlank(CharSequence str) { int length; if (str != null & amp; & amp; (length = str. length()) != 0) { for(int i = 0; i < length; + + i) { if (!CharUtil.isBlankChar(str.charAt(i))) { return false; } } return true; } else { return true; } } public static boolean isNotBlank(CharSequence str) { return !isBlank(str); }