An array of objects in Java

Source: Internet
Author: User

An array of objects, which is easily understood as: the elements in the array are all objects. but this understanding is wrong , the array is not the object itself, but the object's reference, that is, the pointer to the object, and this pointer is stored in the virtual machine memory stack.

Creating an array of objects requires two steps: 1) Create an array that holds references to objects. 2) Specify which object each reference points to (implemented in new). The following examples illustrate.

Suppose you have a class with a class name of test. 1) test[] array = new TEST[2]; Create an array named array with a total of 2 elements, each of which is a reference to the test object. The default initial value for Array[0] and array[1] at this time is null (no point to any object). 2) array[0] = new test (); array[1] = new test (), respectively, in the heap of virtual machine memory, two space to Test two objects, array[0] and array[1] respectively point to the first address of the two areas.

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.