An array of basic skills of programmers and memory control

Source: Internet
Author: User

1. Array initialization

The Java array is static, and once the array is initialized, the length of the array is immutable , and the Java array needs to be initialized for use

The initialization of an array is a contiguous memory space in which an array of objects is allocated a length of arrays in the heap memory, and the initial values are set (note that the array object is not initialized, but not the array variable)

There are two ways to initialize:

1) Static initialization: The programmer displays the initial value of the specified array element, and the system determines the length of the array

2) Dynamic initialization: Only specify the length of the array when initializing, and the system assigns the initial value to the element

The array variable itself is a reference type, stored in the stack memory, and the array object is stored in heap memory

int[] arr1 = new int[]{1,3,4,5};//static int[] arr2 = new int[5];//dynamic

For dynamic initialization, the system gives the initial value according to the following rules:

The type of the array element is the integer in the base type, byte short int long, and the value of the array element is 0;

The type of the array element is a floating-point short double in the base type, and the value of the array element is 0.0;

The type of the array element is a Boolean in the base type Boolean, and the value of the array element is false;

The type of the array element is the character char in the base type, and the value of the array element is ' \u0000 ';

The type of the array element is a reference type, and the value of the array element is null;

for int[] arr; arr = new int[]{2,5,-12,20};

When executing int[] arr; post-code memory

When performing arr = new int[]{2,5,-12,20}; Code after

--------------------------------------------------------------------------------------------------------------- ----------------

--------------------------------------------------------------------------------------------------------------- --------

--------------------------------------------------------------------------------------------------------------- ----------------------

--------------------------------------------------------------------------------------------------------------- ------------------------

--------------------------------------------------------------------------------------------------------------- ---------------------------

An array of basic skills of programmers and memory control

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.