Difference between an array length of zero and a null reference files!=null&files.length () >0 null pointer or null value

Source: Internet
Author: User
Tags array length

Files!=null&files.length () is often used when judging non-empty >0 so many people wonder if the length equals 0 is empty?
To speak concisely:

Array initialization is summed up to two kinds:
null pointer: array =null Empty, is really empty, even the address is not assigned
null value: array ={}; There is no content, but there is space allocated, length 0, with address

public class Testarray {public
    static void Main (string[] args) {
//"Null initialization does not assign address"
//Use statement array must be initialized with NULL initialization , the JVM does not assign an address
//array to array A. ToString () returns the hash value of the address, so a.tostring () does not exist.
//a.length does not exist. Will error
        string[] A = null;  
"Blank initialization length=0 but assigning addresses"
//So there will be all the features of the array b.length=0;  B.tostring () =[ljava.lang.string;@7150bd4d;
        String[] b={};

        System.out.println ("A has no address so there is no length address attribute can only be used to determine whether it is empty:");
        SYSTEM.OUT.PRINTLN ("* * * a==null results are:" + (A==null));
        System.out.println ("B without content but with address so have length address attribute");
        SYSTEM.OUT.PRINTLN ("* * * Length:" +b.length+ "   B's Address:" +b.tostring ());
    }

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.