Instead of returning null, you should return a 0-length array or an empty collection in Java

Source: Internet
Author: User

Note: In order to avoid adding null to the array and collection, it is also possible to reduce unnecessary null pointer exceptions, usually in the business to return a zero group or an empty collection.

Method:

1. Array:

Define global static constants to reduce memory overhead: private static final int empty_array = new Int[0], or, of course, direct local variables and so on.

2. Collection:

Use the tool class collections Emptylist () to put back.

Reference:

http://blog.csdn.net/tounaobun/article/details/8502160

Https://www.codeproject.com/Articles/794448/Is-it-Really-Better-to-Return-an-Empty-List-Instea

In Java, you should return a 0-length array or an empty collection instead of returning null (GO)

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.