一、情景
- 判断输入字符串是否为空
- 分析:null && “” || length()==0
二、区分null 与“”
- null:字符串不指向任何东西,即null不是个对象,null没有分配空间,,未实例化,所以这时候调用它会报空指针异常
- “” :它指向一个长度为0的字符串,即“”是个对象,“”分配了空间;已经实例化
三、应用场景
- 正确写法:if(str==null||str.equals("")) ==>先判断字符串是否为对象,在判断是否为空字符串
- 错误写法:
• if(
str.equals("")
||
str==null
) ==>这种写法容易造成空指针异常!!
- 所以判断字符串是否为空,首先确保它不是NULL,即是个对象,然后再判断它的长度是否为0
- if(str!=null && str.length!=0)
- if( StringUtils.isNotBlank(str) ) ==>使用StringUtils中封装的方法StringUtils.isNotBlank()
一、实践
说明:以下五种方法,都是测试相同字符串“ This is the test data ”,测试循环次数: 10000
当测试数据足够长,次数足够多,差别才会特别明显。反复试验多次,最终得出以下结论
方法 | 代码 | 示范:测试耗时 | 效率 | 名次 | 备注 |
一 | str == null || "" .equals(str) | 694ms | 4 | ||
二 | str == null || str.length() <= 0 | 458ms | 较高 | 2 | |
三 | str == null || str.isEmpty() | 601ms | 次之 | 3 | javaSE6.0之开始提供该方法;才兼容 |
四 | str == null || str == "" | 178ms | 最高 | 1 | |
五 | StringUtils. isBlank (str) | 744ms | 最低 | 5 | 效率最低,StringUtils中封装的 |
总备注 | 当判断的循环的次数较少时,推荐方法四 |
二、实践
说明:以下五种方法,都是测试相同字符串“ This is the test data ”,测试循环次数: 100000
当测试数据足够长,次数足够多,差别才会特别明显。反复试验多次,最终得出以下结论
方法 | 代码 | 示范:测试耗时 | 效率 | 名次 | 备注 |
一 | str == null || "" .equals(str) | 4803ms | 最低 | 5 | |
二 | str == null || str.length() <= 0 | 2979ms | 2 | ||
三 | str == null || str.isEmpty() | 2965ms | 次之 | 3 | javaSE6.0之开始提供该方法;才兼容 |
四 | str == null || str == "" | 2978ms | 较高 | 2 | |
五 | StringUtils. isBlank (str) | 2962ms | 最高 | 1 | StringUtils中封装的 |
总备注 | 当判断的循环的次数较大时,推荐方法五 |
public class StringJudgeController {
public static void main(String[] args) {
// 当测试的次数从10000 变为100000.结果差异很大!!!
JudgeString1("This is the test data", 10000);
JudgeString2("This is the test data", 10000);
JudgeString3("This is the test data", 10000);
JudgeString4("This is the test data", 10000);
JudgeString5("This is the test data", 10000);
}
/**
* 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低;
* 方法二: 比较字符串长度, 效率高, 是最好的一个方法;
* 方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二;
* 方法四: 这是一种比较直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多;
* 方法五: 这是java自己封装的方法,方便调用;但是效率最低
*/
public static void JudgeString1(String str, long num) {
long startTiem = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (str == null || "".equals(str)) {
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("function1耗时:" + (endTime - startTiem) + "ms");
}
public static void JudgeString2(String str, long num) {
long startTiem = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (str == null || str.length() <= 0) {
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("function2耗时:" + (endTime - startTiem) + "ms");
}
public static void JudgeString3(String str, long num) {
long startTiem = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (str == null || str.isEmpty()) {
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("function3耗时:" + (endTime - startTiem) + "ms");
}
public static void JudgeString4(String str, long num) {
long startTiem = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (str == null || str == "") {
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("function4耗时:" + (endTime - startTiem) + "ms");
}
public static void JudgeString5(String str, long num) {
long startTiem = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (StringUtils.isBlank(str)) {
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("function5耗时:" + (endTime - startTiem) + "ms");
}
}