cool hit counter [J2SE Fast Forward] - Arrays (and their memory analysis)_Intefrankly

[J2SE Fast Forward] - Arrays (and their memory analysis)


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/43091009

Early in the study of other languages such as C, VB, C#, C++, etc. also studied the array, usually the project is often used, but never carefully then in-depth understanding, here to learn Java and encountered, re-organized.

A one-sentence summary of what an array is: an array is a collection of data that has a set of the same data type.

Array variables are reference types, and arrays can also be thought of as objects, with each element of the array being equivalent to a member variable of that object. The type of each element in the array can be of any data type.

Array definition and initialization

The declaration of an array can be thought of as the process of allocating space in memory for each element of the array; the initialization of an array can be thought of as the process of assigning values to the allocated space.

Definition of an array

The format of defining an array in Java is: type[] variable name, e.g. int[] arr;

<pre name="code" class="java">int[] arr;
arr=new int[4];

The code int[] arr defines an array variable named arr, allocating only one variable in stack memory with empty contents. As shown below.

The execution of the code arr=new int[4] allocates a space in the variable arr, which allocates an array of length 4 in heap memory (i.e., the set of 4 storage units used to store int types), and the storage units within this array are automatically initialized to 0, just like member variables of a class. As shown below.

Note: Because arrays in Java are in heap memory, arrays in some other languages such as C and C++ can exist in stack memory, unlike C and C++, the length of an array cannot be specified when declaring an array in Java, e.g. int[4] arr; This is the wrong way to write it.

Initialization of arrays

Initializing an array can be done either statically or dynamically.

Static initialization, which is the assignment of values to an array while defining it, e.g.

int[] arr={1,2,3,4};

It could also be written as.

int[] arr=new int[]{1,2,3,4};

Dynamic initialization, which means defining the array first and then assigning values to it when needed, e.g.

<span style="white-space:pre">	</span>int[] arr=new int[5];
<span style="white-space:pre">	</span>arr[0]=1;
<span style="white-space:pre">	</span>arr[1]=2;
<span style="white-space:pre">	</span>arr[2]=3;
<span style="white-space:pre">	</span>arr[3]=4;
<span style="white-space:pre">	</span>arr[4]=5;

Dynamic initialization without assigning values to the elements in the array will Corresponding to its data type Generates the appropriate initial value, the same as the default value of a class member variable, which defaults to False for boolean types, null for reference types, and 0 for other types.

The length of the array must be specified at initialization time, whether static or dynamic initialization.

References to arrays

After defining the array and allocating space for it, you can refer to each element in the array by referencing it as: arr[index]

arr is the array variable name, index is the array subscript, and its value can be an integer constant or an expression of length n. The range of the array subscript is 0 to n-1. For example, when an array a of length n is defined, a[4] represents the 5th number in this array, i.e., the nth data in array a is stored in a[n-1].

In addition, each array in Java has a length property specifying the length of the array, e.g. the value of a.length is the length of the array a.

give an example

☆ Define an integer array

<pre name="code" class="java" style="font-size: 18px;">public class ArrayTest {
	public static void main(String[] args) {
		int arr[]=new int[3];
		arr[0]=1;
		arr[1]=2;
		arr[2]=3;
                arr[3]=4;
	}
}

The memory changes in the code defining arr are the same as the memory map changes for the array defined above, and the final result after assigning values to the four cells in arr is


Recommended>>
1、Installation Diagram for Concerto X10
2、Big data tells you what parts of the country are foodie hotspots R language code explanation
3、8 minutes to teach your girlfriend to read blockchain as well as Xu Xiaoping
4、Laos BST Elephant Chain relaunches on Hash Exchange
5、Qinzhou mahjong cheats Qinzhou mahjong seethrough software Qinzhou mahjong hangout aids

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号