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};
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]; // 动态初始化数组
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.