Does java.util.List.isEmpty() check if the list itself is null?
No, the isEmpty()
method of the java.util.List
interface does not check if the list itself is null
. It only checks if the list is empty, i.e. if it contains no elements.
Here is an example of how the isEmpty()
method can be used:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create an empty list
List<String> list = new ArrayList<>();
// Check if the list is empty
if (list.isEmpty()) {
System.out.println("The list is empty");
} else {
System.out.println("The list is not empty");
}
}
}
This code creates an empty ArrayList and uses the isEmpty()
method to check if it is empty. It prints the appropriate message to the console.
If you want to check if a list is null
, you can use the ==
operator to compare the list to null
. Here is an example:
if (list == null) {
System.out.println("The list is null");
} else {
System.out.println("The list is not null");
}
This code checks if the list is null
and prints the appropriate message to the console.