Why is it that in Java often make list judgment, not only to judge the list is not NULL, but also to judge size>0 it, is not superfluous it?

Source: Internet
Author: User
List list=new ArrayList ();
1. If this is the case, LIS is not null but size=0
Such a collection for subsequent judgments is useless, or even reported anomalies, such as List.get (0.......N) such as the value of judgment, will be reported abnormal.

2. If you first judge the size and then judge null such as:
if (list.size>0) {};
In this case, if the list equals null then List.size will report a null pointer exception

So to double judgment so write best if (list!=null && list.size>0) {};

Why is it that in Java often make list judgment, not only to judge the list is not NULL, but also to judge size>0 it, is not superfluous it?

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.