检查字符串数组元素是否为空
Check if String array element is null
所以我查看了有关此问题的其他一些线程,似乎我是否应该能够使用常规比较运算符来检查它。
How to check if my string is equal to null?
Java, check whether a string is not null and not empty?
然而,即使我的程序说该字符串为空,它后来通过执行条件为该字符串不为空的 if 语句来反驳这一点。为了更清楚,这是我的完整程序:
package bank;
public class HowCheckForNull {
static void showDates(String[] dates){
for(int i = 0; i < dates.length; i++){
System.out.println(dates[i]);
System.out.println(dates[i] == null);
System.out.println(dates[i] == (String) null);
System.out.println(dates[i] != null);
if(dates[i] != null);{ //This should not execute!?
System.out.print("A transaction of X$ was made on the " + dates[i] + "\n");
}
}
System.out.println("");
}
public static void main(String args[]){
String[] dates = new String[3];
showDates(dates);
}
}
输出:
null
true
true
false
A transaction of X$ was made on the null
null
true
true
false
A transaction of X$ was made on the null
null
true
true
false
A transaction of X$ was made on the null
这里有几件事让我感到困惑,为什么 if
语句执行,即使日志记录另有说明,dates[i]
怎么会等于 null
和 (String) null
?
if(dates[i] != null);
^
额外的;导致以下块始终执行(无论 if 语句的评估如何),因为它结束了 if 语句。删除它。
问题是';'在 if(condition);
之后结束语句并以正常方式处理剩余的代码段,而不管任何条件。
代码
package bank;
public class HowCheckForNull {
static void showDates(String[] dates){
for(int i = 0; i < dates.length; i++){
System.out.println(dates[i]);
System.out.println(dates[i] == null);
System.out.println(dates[i] == (String) null);
System.out.println(dates[i] != null);
if(dates[i] != null){ //Now it will not execute.
System.out.print("A transaction of X$ was made on the " + dates[i] + "\n");
}
}
System.out.println("");
}
public static void main(String args[]){
String[] dates = new String[3];
showDates(dates);
}
}
输出
null
true
true
false
null
true
true
false
null
true
true
false
所以我查看了有关此问题的其他一些线程,似乎我是否应该能够使用常规比较运算符来检查它。
How to check if my string is equal to null?
Java, check whether a string is not null and not empty?
然而,即使我的程序说该字符串为空,它后来通过执行条件为该字符串不为空的 if 语句来反驳这一点。为了更清楚,这是我的完整程序:
package bank;
public class HowCheckForNull {
static void showDates(String[] dates){
for(int i = 0; i < dates.length; i++){
System.out.println(dates[i]);
System.out.println(dates[i] == null);
System.out.println(dates[i] == (String) null);
System.out.println(dates[i] != null);
if(dates[i] != null);{ //This should not execute!?
System.out.print("A transaction of X$ was made on the " + dates[i] + "\n");
}
}
System.out.println("");
}
public static void main(String args[]){
String[] dates = new String[3];
showDates(dates);
}
}
输出:
null
true
true
false
A transaction of X$ was made on the null
null
true
true
false
A transaction of X$ was made on the null
null
true
true
false
A transaction of X$ was made on the null
这里有几件事让我感到困惑,为什么 if
语句执行,即使日志记录另有说明,dates[i]
怎么会等于 null
和 (String) null
?
if(dates[i] != null);
^
额外的;导致以下块始终执行(无论 if 语句的评估如何),因为它结束了 if 语句。删除它。
问题是';'在 if(condition);
之后结束语句并以正常方式处理剩余的代码段,而不管任何条件。
代码
package bank;
public class HowCheckForNull {
static void showDates(String[] dates){
for(int i = 0; i < dates.length; i++){
System.out.println(dates[i]);
System.out.println(dates[i] == null);
System.out.println(dates[i] == (String) null);
System.out.println(dates[i] != null);
if(dates[i] != null){ //Now it will not execute.
System.out.print("A transaction of X$ was made on the " + dates[i] + "\n");
}
}
System.out.println("");
}
public static void main(String args[]){
String[] dates = new String[3];
showDates(dates);
}
}
输出
null
true
true
false
null
true
true
false
null
true
true
false