Array --- array is the first level object!

Source: Internet
Author: User
Arrays are objects:

No matter which type of array is used, the array identifier is actually a reference pointing to a real object created in the heap. This array object is used to save references pointing to other objects.

This object can be implicitly created as part of the array initialization syntax or as displayed in the new expression.

The length of a read-only member is a part of an array object (in fact, it is the only field or method that can be accessed), indicating how many elements the array object can store. The [] syntax is the only way to access array objects.

Various methods for initializing arrays & Use of object arrays and basic types of Arrays:

The following example summarizes various methods for initializing arrays. It also shows that object arrays and basic types arrays are almost identical in usage;

The only difference is that the object array stores the reference, and the basic type array directly stores the value of the basic type.

Class berylliumsphere {Private Static long counter; // static variables belong to the class and are unique. Private Final long id = counter ++; // The final variable value cannot be changed to Public String tostring () {return "sphere" + id ;}} public class main {public static void main (string [] ARGs) {/*** object array **/berylliumsphere []; // create an object array reference berylliumsphere [] B = new berylliumsphere [5]; // create an array object reference, pointing to a real object created in the heap // All references to the real array in the heap will be automatically initialized to null // B: [null, null] system. out. println ("B:" + arrays. tostring (B); berylliumsphere [] C = new berylliumsphere [4]; // For (INT I = 0; I <C. length; I ++) {If (C [I] = NULL) {// test whether null references C [I] = new berylliumsphere ();}} // clustering initialize berylliumsphere [] d = {New berylliumsphere (), new berylliumsphere (), new berylliumsphere ()}; // initialize a = new berylliumsphere [] {New berylliumsphere (), new berylliumsphere (), new berylliumsphere ()};/*. length = 3B. length = 5C. length = 4D. length = 3A. length = 3 */system. out. println (". length = "+. length); // length is the size of the array, rather than the number of actually saved elements. out. println ("B. length = "+ B. length); system. out. println ("C. length = "+ C. length); system. out. println ("D. length = "+ D. length); A = D; system. out. println (". length = "+. length);/*** basic data type array **/INT [] E; // null reference int [] f = new int [5]; // It is automatically initialized to 0system. out. println ("F:" + arrays. tostring (f); int [] G = new int [4]; for (INT I = 0; I <G. length; I ++) {G [I] = I * I;} int [] H = {11, 47, 91};/* F: [0, 0, 0, 0, 0] F: [0, 0, 0, 0] G: [0, 1, 4, 9] H: [11, 47, 91] E: [11, 47, 91] E: [1, 2] */system. out. println ("F:" + arrays. tostring (f); system. out. println ("G:" + arrays. tostring (g); system. out. println ("H:" + arrays. tostring (h); E = H; system. out. println ("E:" + arrays. tostring (e); E = new int [] {1, 2}; system. out. println ("E:" + arrays. tostring (e ));}

Array B is initialized to point to an array referenced by a berylliumsphere, but no berylliumsphere object is actually placed in the array.

However, you can still ask about the size of the array because B points to a valid object. There is a small drawback: you cannot know the exact number of elements in this array,

Because length only indicates how many elements the array can accommodate. That is to say, length is the size of the array, rather than the number of actually stored elements.

When an array object is generated, all references are automatically initialized to null. Similarly, if an array of the basic type is numeric, it is automatically initialized to 0, char of the numeric type, 0 for the automatic initialization, and false for the boolean type.

Result:

The basic type array works in the same way as the object array, but the basic type array directly stores the value of the basic type data.

Array --- array is the first level object!

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.