This article mainly introduces the method of judging whether an object is empty (including null, "") in Java. The article introduces it in great detail through sample codes. It has a certain reference learning value for everyone's study or work. Friends who need it will follow along below. Come and study with me
directory title
What needs to be understood to judge between objects
Before writing a method, take a look at this example to see if you really understand JAVA objects.
code example
The judged Java object: Student (Student .java)
public class Student {
private String name; //姓名
private String age; //年龄
private String sex; //性别
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Things to note when judging whether an object is empty:
// 判断1 : 这里会返回 true
Student student = null;
if(student == null){
return true;
}else{
return false;
}
// 判断2 : 这里会返回 false
Student student = new Student ();
if(student == null){
return true;
}else{
return false;
}
problem causes
Student student = new Student ();
At this time, an object has been created, so the student will not be null. When the object new is created, although its property value is empty, the object will no longer be empty. When we output student.toString(), we will find that it will output something like the following, which means that the object is not empty and belongs to the instantiated object.
com.example.demo.text.Student@15db9742
We can also override the toString method to let him print all attributes
Notice
Student student = null; When the object is a Null value, it cannot toString and get, set its attributes. Otherwise, a null pointer will be reported. If you want to do these operations, first create a new object to ensure that the object is not in a null state.
Exception in thread "main" java.lang.NullPointerException
at com.example.demo.text.Test.main(Test.java:13)
Method to determine if an object is empty
In many cases in the project, the data obtained through the interface will receive a Map<String, Object> or JSONObject type of data, and then get a certain attribute, such as get("personInfoId"). At this time, if the name attribute is null, toString will report an error.
@RequestMapping(value = "/personInfoMoveIn", method = RequestMethod.POST)
public ResultDto personInfoMoveIn(@RequestBody Map<String, Object> params) {
String personInfoId = params.get("personInfoId").toString();
return personInfoService.personInfoMoveIn(params);
}
solution
params.get("personInfoId") == null ? "" : params.get("districtId").toString();
Because some conditions are not mandatory, sometimes the front desk will send us an empty string instead of null. At this time, we can write a public method to judge this object. The following two methods are for reference.
the first method
public static boolean isBlankOrEmpty(Object object) {
if (null == object) {
return false;
}
if (object.toString().equals("")) {
return false;
}
return true;
}
The second method
The ObjectUtils and StringUtils tool classes in the lang package are used in combination to determine whether the Object object is empty or an empty string
public static Boolean isObjectNotEmpty(Object obj) {
String str = ObjectUtils.toString(obj, "");
return StringUtils.isNotBlank(str);
}
Summarize
I believe that bloggers who have read from the beginning to the end should have a better understanding of the subject . If you like the blogger, you can pay attention to it. We can make progress and learn together, and live hard!