Java array initialization and memory control

Source: Internet
Author: User

One, the Java array is static

Java is a typical static language, the Java array is also static, that is, when the array is initialized, the length of the array is immutable;


Second, What is the initialization of the array

Array initialization is the allocation of contiguous memory space to an array object in heap memory and an initial value for each array element.


three, array initialization way

There are two ways of doing this:

1. Static initialization: The programmer explicitly specifies the initial value of each array element, and the size of the array is determined by the system;

2, dynamic initialization: By the programmer to specify the size of the array, the system for each array elements to allocate the initial value;


four, array variables and array objects


Array variable:

An array variable is a reference variable type (essentially a pointer), created in the stack area (in Java, where all local variables are stored in the stack, regardless of the basic data type (int, short, Long, Byte, Boolean, char, float, etc.) variable, or reference type variable; Java in heap memory objects usually do not allow direct access, in order to access the object, only through the stack area reference object

If string[] books = new String[5], books are the array variables;


Array objects:

The array object is a " true array ", created in heap memory ;

For an array object of the base type, the data element value is stored directly in the allocated memory space, and for an array object of a complex type (such as String), the allocated memory space is also referenced, referencing (pointing to) Chang (Constant Pool) or other memory area objects;

such as string[] books = new string[5]; new String[5] is an array object;


The relationship between them:

The array variable, referencing (pointing to) the array object in the heap memory;


Summary:

so please always remember: the Java array variable is a reference-type variable that is not an array object itself and does not require initialization, so long as the array variable points to a valid array object in heap memory, the program can use the array variable, which is substituted by the object it references, The system automatically changes to access the corresponding array object in the heap memory.


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.