检查String数组元素是否为null

Pan*_*pas 0 java arrays string null

所以我已经查看了一些关于这个问题的其他线程,似乎我应该能够使用常规比较运算符来检查这个问题.

如何检查我的字符串是否等于null?

Java,检查字符串是否为空且不为空?

但是,即使我的程序声明字符串为null,它也会通过执行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);
    }
    }
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

几件事情困扰我在这里,为什么是if即使记录表明并非如此声明执行,以及它如何可以是dates[i]等于既null(String) null

Era*_*ran 10

if(dates[i] != null);
                    ^
Run Code Online (Sandbox Code Playgroud)

额外的; 导致以下块始终执行(无论if语句的评估如何),因为它结束if语句.去掉它.