To check if an element of an array is null
in Java, you can use the ==
operator to compare the element to the null
value.
Here's an example of how to check if an element of an array is null
:
String[] array = new String[5]; if (array[0] == null) { // element is null } else { // element is not null }
In this example, the if
statement will evaluate to true
if the first element of the array
is null
, and false
otherwise.
You can also use the ==
operator to check if an element of an array of any type is null
. For example:
Object[] array = new Object[5]; if (array[0] == null) { // element is null } else { // element is not null }
Note that the ==
operator checks for reference equality, which means that it will return true
if the element is null
and false
if the element is any other value, including an object that has a value of null
.
If you want to check if an element of an array is null
or has a value of null
, you can use the ==
operator or the .equals
method, depending on the type of the element. For example:
String[] array = new String[5]; if (array[0] == null || array[0].equals(null)) { // element is null or has a value of null } else { // element is not null }