Java Null Check Methods
There are several methods in Java to determine if an object is empty.
- Use the == operator to check if an object is null. For example: if (object == null) { … }
- Use the equals() method to check if an object is null. For example: if (object.equals(null)) { … }
- Check if the object is null using the Objects.isNull() method. For example: if (Objects.isNull(object)) { … }
- Use the Objects.nonNull() method to check if an object is not null. For example: if (Objects.nonNull(object)) { … }
- Use the StringUtils.isEmpty() method to check if a string is empty. For example: if (StringUtils.isEmpty(string)) { … }
- To check if a collection is empty, use the Collection.isEmpty() method. For example: if (collection.isEmpty()) { … }