There are following way to Check if the variable of an object is null or empty in java.
- Inside isData(Student request) method return type is Boolean.
- If statements validate request.getName() is null or empty and in the end, they return the boolean status.
- Print the checkStatus value.
import java.util.Objects;
class Student {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class CheckData {
public static void main(String[] args) {
Student s1 = new Student();
s1.setId(12);
s1.setName("");
boolean checkStatus = isData(s1);
System.out.println("check object is empty or null status :- " + checkStatus);
}
private static boolean isData(Student request) {
if (Objects.isNull(request.getName()) || request.getName().isEmpty()) {
return false;
} else {
return true;
}
}
}
Output :-
check object is empty or null status :- false