- 添加依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
- 当字符串为空串时候:
import static com.google.common.base.Strings.isNullOrEmpty;
String str = "";
System.out.println(str.isEmpty());
// true
System.out.println(isNullOrEmpty(str));
// true
- 当字符串为null时候:
import static com.google.common.base.Strings.isNullOrEmpty;
String str = "";
System.out.println(str.isEmpty());
Exception in thread "main" java.lang.NullPointerException
// at util.NullTest.main(NullTest.java:12)
System.out.println(isNullOrEmpty(str));
// true
3.jdk8特性,空指针? empty() : of(value);
String str = null;
System.out.println( Optional.ofNullable(str).orElse("1"));
//1