Well-off accompany you to learn about the Java--------array

Source: Internet
Author: User

today, we share an important data type-array in Java learning.

Using arrays is a pretty good choice if you want to store a series of related data. In addition, if a program fragment is frequently repeated, defining it as a method can effectively simplify the program code. An array is a data type that consists of a set of variables of the same type, represented by a common name, and the individual elements in the array are labeled to indicate where they are stored.

To use an array of Java, you must go through two steps: (1) declare an array, (2) Allocate memory to the array.

The syntax for these two steps is as follows:

In the declaration format of an array, the "data type" is the data type that declares the array elements, and the common types are integer, float, and character. "Array Name" is the name of the element used to unify this set of identical data types, with the same naming rules and the same, it is recommended that the reader use meaningful names for the array name. After the array declaration, the next step is to configure the memory required for the array, where "number" tells the compiler how many elements the array is declared to hold, and "new" is the command compiler to create a chunk of memory in memory for the array to use, based on the number of parentheses. Here is an example of declaring a one-dimensional array and allocating memory to that array:

int score[]; Declaring an integer array score

Score = new Int[3]; Allocates memory space for an integer array score with a number of 4 elements

In the first row of the previous example, when declaring an integer array score, score is visible as a variable of the array type, at which point the variable does not contain anything, and the compiler allocates only a piece of memory to it to hold the address to the array entity:

After the declaration, the memory allocation operation is done, which is the second line in the previous example. This line opens up 3 memory spaces that can be used to hold integers, and assigns the reference address of this memory space to the score variable. The flow of its memory allocation:

The memory reference address in Ox1000 is a false assignment, and this value can vary depending on the environment. Arrays are non-basic data types, so the array variable score is not the entity of the array, but the reference address of the array entity. In addition to using the above two lines to declare and allocate memory to the array, you can also use a more concise way, the two lines are indented one line to write, the format is as follows:

The above format, when declared, allocates a piece of memory space for use by the array. The following example declares an integer array score and opens a memory that can hold 11 integers to the score variable.

int score[] = new INT[11];

Declares an integer array score with a number of 10 elements, while opening up a memory space for its use

In Java, because the integer data type occupies 4 bytes, and the integer array score holds 11 elements, the memory used in the example above is 4 * 11 = 44 bytes.

The above is a simple introduction to the array, in the Java programming work, will often use arrays, especially the array of memory allocation space, may not be very understanding at the beginning, I will continue to share with you some examples of the array.

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.