Home > Java > Javagetting Started > How to initialize an array in java

How to initialize an array in java

爱喝马黛茶的安东尼
Release: 2019-11-14 16:40:15
Original
15480 people have browsed it

How to initialize an array in java

1. What is initialization

In Java program development, arrays are initialized before they are used. 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. The so-called array initialization is the process of making the array name point to the array object. This process is mainly divided into two steps. The first 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, assigning the array name a reference to the array object.

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

2. 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 values, separated by 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.

3. 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. Additionally, when an array is dynamically initialized, its elements are set to default initial values ​​based on their data type. The default value of each element in the array in this example is 0. The default values ​​of other common data types are as shown in the following table:

How to initialize an array in java

numerousjava training videos , all on the PHP Chinese website, welcome to learn online!

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