J2SE Fast Advanced--arrays (and their memory analysis)

Source: Internet
Author: User
Tags array definition


As early as learning other languages such as C, VB, C #, C + + and so also learn several groups, usually in the project also often used, but never careful to understand, here to learn Java and encountered, and re-organized.

One sentence summarizes what an array is: An array is a collection of data with the same set of data types.

An array variable is a reference type, and an array can be considered an object, and each element in the array is equivalent to the member variable of that object. The type of each element in the array can be any data type.


array definition and initialization

The declaration of an array can be considered to be the process of allocating space for each element of an array in memory, and the initialization of arrays can be considered as the process of assigning values to the allocated space.


definition of an array        

the format of the defined array in Java is: type [] variable name, such as int[] arr;                              

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

Int[] Arr This code defines an array variable named arr, allocating only one variable with empty contents in the stack memory. Such as:

Arr=new Int[4] The execution of this code allocates a space in the variable arr, which allocates an array of length 4 in the heap memory (that is, 4 sets of storage units used to store the int type), and the storage units within the array are automatically initialized to 0. Is the same as the member variable of the class. such as:

Note: Because arrays in Java are present in heap memory, other languages such as arrays in C, C + + can exist in stack memory, and do not specify the length of an array when declaring an array in C and C++,java, such as int[4] arr; This is the wrong wording.

initialization of the array

Initializing an array can be statically initialized or dynamically initialized.

static initialization is the assignment of a value to an array at the same time that it is defined, such as:       

Int[] arr={1,2,3,4};
Can also be written as:     

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

Dynamic initialization is the definition of an array, which is then assigned when needed, such as:      

<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, if you do not assign a value to an element in an array, you corresponds to its data type generates the corresponding initial value, as with the default value of the member variable of the class, the Boolean type defaults to False, the reference type defaults to NULL, and the other type defaults to 0.

Whether it is static initialization or dynamic initialization, the length of the array must be specified when initializing.

references to arrays

Once you have defined the array and allocated space for it, you can refer to each element in the array as follows: Arr[index]

ARR is an array variable name, index is subscript, and its value can be an integer constant or the range of subscript with an expression length of n is 0~n-1. If an array of length n is defined, A[4] represents the 5th number in the array, that is, the nth data in array A is stored in a[n-1].

In addition, each array in Java has a length property that indicates how long the array is, such as the value of a.length is the length of array A.


Examples Show

☆ 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 for arr defined in the code are the same as in the memory diagram of the array defined above, and the result is that the four cells in Arr are assigned the following values:


J2SE Fast Advanced--arrays (and their memory analysis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.