In Java, you can use the following methods to determine whether the object is empty:
1. Use==
or!=
Operator comparison object references andnull
. For example:
if (myObject == null) { // myObject is empty} else { // myObject is not empty}
2. Use()
method. The advantage of this method is that it can be handlednull
Parameters will not be thrownNullPointerException
. For example:
if ((myObject)) { // myObject is empty} else { // myObject is not empty}
3. Use the objectinstanceof
operator. This method can check whether the object is of a specific type. For example:
if (myObject instanceof MyClass) { // myObject is not empty and is of type MyClass} else { // myObject is empty or not MyClass type}
4. UseOptional
kind. This is a powerful tool class introduced in Java 8 that can handle null values gracefully. For example:
Optional<MyClass> myOptional = (myObject); if (()) { // myObject is not empty MyClass myInstance = (); } else { // myObject is empty}
In general, which method to choose depends mainly on specific requirements and coding habits. It should be noted that use it directly==
or!=
Operator comparison object references andnull
is the easiest and most direct way, but may be thrown in some casesNullPointerException
. use()
andOptional
Classes can handle null values more safely.
Attachment: It is determined that the attribute value of an object is not empty/at least one attribute value is not empty
If it is a string property, it is not null and not ""
Sometimes, the controller parameter receives a json object. We need to determine whether the attribute values in the object are non-empty (all are necessary)/at least one attribute value is not empty, where the string attribute is not null and not "". If there are many parameters in the object, the controller will traverse each attribute to judge, and the code is redundant.
Of course there are other methods, such as the annotations @NotNull, @NotBlank, @Valid in the spring framework.
import ; import ; import ; /** * Title: determine that the value of an object's attribute is not empty, the string attribute is not empty or the string is not empty * Description: Use the reflection of the class to obtain, provided that the properties in the javabean are all wrapped classes * @author WZQ * @version 1.0.0 * @date 2020/4/19 */ public class ObjIsUtil { /** * Get attribute value according to attribute name */ private static Object getFieldValueByName(String fieldName, Object o) { try { String firstLetter = (0, 1).toUpperCase(); String getter = "get" + firstLetter + (1); Method method = ().getMethod(getter, new Class[]{}); Object value = (o, new Object[]{}); return value; } catch (Exception e) { return null; } } /** * To determine whether the attribute values in the object are not empty, the string attribute needs to determine the empty string */ public static Boolean isAllNotNull(Object o) { //Get the object's attribute array, reflect Field[] fields = ().getDeclaredFields(); String name = ""; for (int i = 0; i < ; i++) { //Property name name = fields[i].getName(); //pojos and dto contain serialVersionUID, so the judgment of this field must be removed if (("serialVersionUID")) continue; //The value of the current attribute name Object fieldValueByName = getFieldValueByName(name, o); //The value is null if (fieldValueByName == null) return false; //If it is a String, determine the empty string "" if (fields[i].getType().equals()){ String s = (String) fieldValueByName; if ((s)){ return false; } } } return true; } /** * It is determined that at least one of the attribute values in the object is not empty */ public static Boolean oneNotNull(Object o) { //Get the object's attribute array, reflect Field[] fields = ().getDeclaredFields(); String name = ""; for (int i = 0; i < ; i++) { //Property name name = fields[i].getName(); //The value of the current attribute name Object fieldValueByName = getFieldValueByName(name, o); //If it is a String, determine the empty string "" if (fields[i].getType().equals()){ String s = (String) fieldValueByName; if (!(s)){ return true; } }else{ //The value is null if (fieldValueByName != null) return true; } } return false; } // public static void main(String[] args) { // Usertest usertest = new Usertest("1","name",1); // Usertest usertest1 = new Usertest("","",null); // Usertest usertest2 = new Usertest("1","",null); // Usertest usertest3 = new Usertest("1",null,1); // (isAllNotNull(usertest));//true // (isAllNotNull(usertest1));//false // (isAllNotNull(usertest2));//false // (isAllNotNull(usertest3));//false // (oneNotNull(usertest));//true // (oneNotNull(usertest1));//false // } }
Summarize
This is the end of this article about the four methods of judging whether Java objects are empty. For more related content on judging whether Java objects are empty, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!