Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of array initialization in java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "detailed explanation of array initialization in java". In daily operation, I believe that many people have doubts about the detailed explanation of array initialization in java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "detailed explanation of array initialization in java". Next, please follow the editor to study!

Introduction

Learning Java friends must be no stranger to the array, it needs to use it before initialization, this is because the array is a reference type, declare the array is only a reference type of variable, not the array object itself, as long as the array variable points to a valid array object, the program can use the array variable to access array elements. The so-called array initialization is the process of making the array name point to the array object, which is mainly divided into two steps, one is to initialize the array object, that is, to allocate memory space and assign values to the elements in the array, and the other is to initialize the array name. that is, the array name is assigned as a reference to the array object.

Now that you know what array initialization is, let's take a look at how to initialize an array.

Static initialization

Syntax:

Abbreviated format

Data type [] array name = {value, value, … }

Complete format

Data type [] array name = new data type [] {value, value,... }

Note:

1. Static initialization assigns a value to each element of the array when initializing the array, and the length of the array is determined by the system.

two。 The element is fetched by the array subscript, which starts from 0 by default. Array subscript exceeds array length, array crosses bounds exception (runtime exception)

3. The array belongs to the reference data type, so you must open up space (instantiation) before using it, otherwise "NullPoninterException" will be generated.

Array statically initializes real columns:

Int [] arrays = {1,2,3,4}

Int [] arrays = new int [] {1,2,3,4}

Dynamic initialization

Syntax: array type [] array name = new data type [array length]

Note: after dynamic initialization opens up space, each element in the array is the default value for this data type.

For example: integer: byte, short, int, long, the default value is 0; the default value of reference data type is null

Decimal type: default value of foat and double is 0.0

Array dynamically initializes the real column:

Int [] array = new int [5]; / / define and open up an array of length 5 / / assign values to the array through the array subscript (note: array subscript starts at 0) arrays [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5; at this point, the study on "detailed explanation of array initialization in java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report