search
HomeJavaJavagetting StartedHow to initialize an array in java

How to initialize an array in java

Nov 14, 2019 pm 04:40 PM
javainitializationarray

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};

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

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools