Home > Java > javaTutorial > body text

How to initialize an array in java

(*-*)浩
Release: 2019-11-12 10:27:45
Original
3800 people have browsed it

How to initialize an array in java

What is initialization

In Java program development, arrays will be initialized before using them, this is Because arrays are reference types, declaring an array only declares a reference type variable, not the array object itself. As long as the array variable points to a valid array object, the array variable can be used in the program to access the array elements. (Recommended learning: java course)

The so-called array initialization is the process of letting the array name point to the array object. This process is mainly divided into two steps. One is to initialize the array object, that is Allocate memory space and assign values ​​to the elements in the array. The second is to initialize the array name, that is, assign the array name to a reference to the array object.

Arrays can be initialized in two ways, namely static initialization and dynamic initialization.

Static initialization

Static initialization means that the programmer assigns a value to each element of the array when initializing the array, and the system determines the length of the array. .

There are two ways to statically initialize an array. The specific examples are as follows:

array = new int[ ]{1,2,3,4,5};
int[ ] array = {1,2,3,4,5};
Copy after login

The above two methods can achieve static initialization of the array, in which the curly braces contain the array element value, and the element value Separate them with commas ",". Note here that simplified static initialization is only supported when array initialization is performed at the same time as the array is defined. For simplicity, it is recommended to use the second method.

Dynamic initialization

Dynamic initialization means that the programmer specifies the length of the array when initializing the array, and the system assigns initial values ​​to the array elements.

Dynamic initialization of arrays, specific examples are as follows:

int[ ] array = new int[10]; // 动态初始化数组
Copy after login

The format in the above example will allocate a memory space for the use of the array when the array is declared. The length of the array is 10. Since each The elements are all int data types, so the memory occupied by the array in the above example is 10*4=40 bytes. In addition, when an array is dynamically initialized, its elements are set to default initial values ​​based on its data type.

The above is the detailed content of How to initialize an array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template