Business parameters are empty and judged null

Source: Internet
Author: User
Problem with null:

Java types have base variable types, reference types.

(1) for the underlying variable type, if the initialization of Java will automatically open up space and assign values. int, byte, short, Long is 0; float, double is 0.0; Boolean is false.
(2) For reference variable types, Java will also automatically assign values to uninitialized variables, null.
(because null refers to an indeterminate object, it applies to a reference type and does not apply to the underlying variable type.) )

Besides, NullPointerException anomaly.
Pointer is a reference to an object in Java. Like string A; this a is the pointer.
null pointer nullpointer, the contents of the pointer is empty, such as pointer A, if it points to null, is a null pointer.
NULL pointer exception nullpointerexception, one pointer is null. The object itself is empty, there is no way to call you, you are using the object's method, it is too pushy. Again, uninitialized variables Java automatically complains. For example, String a=null, you also use a method, such as A.equals (String x), which produces null pointer exceptions.
The project encountered a lot of nullpointerexception problems, roughly focus on the situation:

(1) An object reference is defined, but no new is used directly. For example, string a = Null,list List = null;
(2) If NULL is not considered, the object directly fetched or returned is empty. Use the object directly. Empty question:

Null values generally refer to an object's parameter value as an empty string, such as obj.value=, and Null generally refers to an object as null, i.e. obj = null;

An object is null and must not have all its parameter values; If the object is not empty, the value of one of its parameters may be empty.
Like what

User user1 = null;
User1.getid () =null//record does not exist
user User2 = new user ();
User2.getid () = '//record exists, but value is null
Reuse judgment in a project:

(1) String
Type is not null and empty

if (Utils.isempty (strparentid) {
            throw new Exception ("parameter cannot be null.) ");
        }

Source:

public static Boolean IsEmpty (String value) {
        if (value = null | | value.isempty ()) {return
            true;
        }

        return false;
}

   public Boolean IsEmpty () {return
        value.length = = 0;
    }

(2) Integer integer

if (id = = NULL) {
            throw new Exception ("parameter cannot be null.") ");
        }

Here I used to use the "". Quals (ID) to the empty, the team laughed for a small half year. Integral type does not require "". equal (ID), the integer is not empty. The integer type is used in the project, and the integer is a wrapper class that includes the base type int and is NULL when assigned. The int basic type initialization is not empty. In both cases, the integer can only be judged by the null value.

(3) The list type is not empty

if (Collectionutils.isempty (list) {
    throw new Exception ("parameter exception.) ");
}

Source:

    public static Boolean IsEmpty (Collection<?> Collection) {return
            (Collection = = NULL | | collection.isempty ());
    }

      public Boolean IsEmpty () {return
            size () = 0;
        }

(3) Map is not empty

if (Collectionutils.isempty (Existmap)) {

}
if (Existmap.isempty ()) {
    //This will also cause an error.
}

Recommend an article:
http://blog.csdn.net/revivedsun/article/details/46581691

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.