Java中關(guān)于isEmpty方法、null以及““的區(qū)別
關(guān)于isEmpty方法、null以及““的區(qū)別
這是一個(gè)比較容易混淆的概念,為了弄清楚這個(gè)問題,最好的方法當(dāng)然是寫程序來驗(yàn)證,開門見山,上代碼!
public class TestNull {
public static void main(String[] args) {
String a = new String();
String b = "";
String c = null;
if (a.isEmpty()) {
System.out.println("String a = new String");
}
if (b.isEmpty()) {
System.out.println("String b = \"\"");
}
if (c == null) {
System.out.println("String c = null");
}
if (null == a) {
System.out.println("String a = null");
}
if (a == "") {
System.out.println("a = ''");
}
if (a.equals("")) {
// 由于a是字符串,字符串的比較需要用equals,不能直接用 ==
System.out.println("a.equals(\"\") is true");
}
/*if (c.isEmpty()) {
// 這里會(huì)報(bào)空指針,即null不能使用此方法
System.out.println("c == null and c.isEmpty");
}*/
List<String> list = new ArrayList<>();
// list.add("");
if (list.isEmpty()) {
System.out.println("list is empty");
}
System.out.println(list.size());
}
}控制臺(tái)輸出

分析
- 此時(shí)a是分配了內(nèi)存空間,但值為空,是絕對(duì)的空,是一種有值(值存在為空而已)。
- 此時(shí)b是分配了內(nèi)存空間,值為空字符串,是相對(duì)的空,是一種有值(值存在為空字串)。
- 此時(shí)c是未分配內(nèi)存空間,無值,是一種無值(值不存在)。
綜上所述
| isEmpty() | 分配了內(nèi)存空間,值為空,是絕對(duì)的空,是一種有值(值 = 空) |
| "" | 分配了內(nèi)存空間,值為空字符串,是相對(duì)的空,是一種有值(值 = 空字串) |
| null | 是未分配內(nèi)存空間,無值,是一種無值(值不存在) |
Java如何快速判null或““
前期測(cè)試代碼
package com.study.string;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;
/**
* @ClassName StringTest
* @Description Java中如何判null或""
* @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.判null + 判"":直觀、方便,但效率極低
if(nullString == null || nullString == ""){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString == ""){// 我是 !
System.out.println("我是 !");
}
// 1.判null + 判"":判""前要保證字符串不為null,否則調(diào)用String方法時(shí)會(huì)拋出空指針異常
if(nullString == null || nullString.length() == 0){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString.length() == 0){// 我是 !
System.out.println("我是 !");
}
// 2.判null + 判"":判""前要保證字符串不為null,且要將非null的字符串放在前面,否則會(huì)產(chǎn)生空指針異常
if(nullString == null || "".equals(nullString)){//我是null!
System.out.println("我是null!");
}
if(emptyString == null || "".equals(emptyString)){// 我是 !
System.out.println("我是 !");
}
// 3.判null + 判"":判""前要保證字符串不為null,否則調(diào)用String方法時(shí)會(huì)拋出空指針異常
if(nullString == null || nullString.isEmpty()){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString.isEmpty()){// 我是 !
System.out.println("我是 !");
}
/**
* 底層源碼:
* private final char value[];
* public boolean isEmpty() {
* return value.length == 0;
* }
* 底層還是調(diào)用數(shù)組的length方法,前提是不為null
*/
// 4.判null + 判""
if (StringUtils.isEmpty(nullString)) {// 我是null!
System.out.println("我是null!");
}
if (StringUtils.isEmpty(emptyString)) {// 我是 !
System.out.println("我是 !");
}
/**
* 底層源碼:
* @Deprecated
* public static boolean isEmpty(@Nullable Object str) {
* return str == null || "".equals(str);
* }
* 底層是分別判斷字符串是否為null或者為“”
*/
// 5.Hutool中的isEmpty方法:判null + 判""
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);
/**
* 底層源碼:
* public static boolean isEmpty(CharSequence str) {
* return str == null || str.length() == 0;
* }
* 底層是分別判斷字符串是否為null或者為“”
*/
// 6.Hutool中的isBlank方法:判null + 判""
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);
/**
* 底層源碼:
* public static boolean isBlank(CharSequence str) {
* int length;
* if (str != null && (length = str.length()) != 0) {
* for(int i = 0; i < length; ++i) {
* if (!CharUtil.isBlankChar(str.charAt(i))) {
* return false;
* }
* }
* return true;
* } else {
* return true;
* }
* }
*/
/**
* 推薦使用Hutool工具類的isEmpty或isBlank方法:
*/
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
}
}心得
實(shí)際應(yīng)用中推薦使用Hutool工具類中的StrUtil類下方法(isEmpty、isNotEmpty、isBlank、isNotBlank)進(jìn)行判null或者判"",對(duì)應(yīng)源碼如下:
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 && (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);
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java TimedCache 帶時(shí)間緩存工具類詳解使用
工具類是包含集合框架、遺留的 collection 類、事件模型、日期和時(shí)間設(shè)施、國際化和各種實(shí)用工具類(字符串標(biāo)記生成器、隨機(jī)數(shù)生成器和位數(shù)組、日期Date類、堆棧Stack類、向量Vector類等)。集合類、時(shí)間處理模式、日期工具等各類常用工具包,本文將介紹帶時(shí)間緩存工具類2021-10-10
通過實(shí)例講解springboot整合WebSocket
這篇文章主要介紹了通過實(shí)例講解springboot整合WebSocket,WebSocket為游覽器和服務(wù)器提供了雙工異步通信的功能,即游覽器可以向服務(wù)器發(fā)送消息,服務(wù)器也可以向游覽器發(fā)送消息。,需要的朋友可以參考下2019-06-06
解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問題
這篇文章主要介紹了解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java Web程序中利用Spring框架返回JSON格式的日期
這里我們來介紹一下Java Web程序中利用Spring框架返回JSON格式的日期的方法,前提注意使用@DatetimeFormat時(shí)要引入一個(gè)類庫joda-time-版本.jar,否則會(huì)無法訪問相應(yīng)路徑2016-05-05
JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例
本篇文章主要介紹了JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
Java-Io-RandomAccessFile任意位置讀寫數(shù)據(jù)的操作小結(jié)
RandomAccessFile類支持隨機(jī)訪問方式,可以跳轉(zhuǎn)到文件的任意位置讀寫數(shù)據(jù),這個(gè)類在文件隨機(jī)讀取時(shí)有很大的優(yōu)勢(shì),可利用多線程完成對(duì)一個(gè)大文件的讀寫,本文給大家介紹Java-Io-RandomAccessFile(任意位置讀寫數(shù)據(jù))的相關(guān)知識(shí),需要的朋友可以參考下2022-05-05

