"Java" resolves an array in Java

Source: Internet
Author: User

Directory structure:

contents Structure [-]
    1. One-dimensional arrays
      1. 1, what is a one-dimensional array
      2. 2, three ways to declare a one-dimensional array
    2. Two-dimensional arrays
      1. 1, what is a two-dimensional array
      2. 2, 3 ways to declare a two-dimensional array
      3. 3, example of a two-dimensional array traversal
    3. The allocation of arrays in memory space
    4. Default initial values for various data types after the declaration is complete
    5. Parse the length property in an array
      1. is the array in Java an object?
      2. Where is the template where the array object is created
      3. The source of the. Lenght property in the Java array.
    6. Reference articles
1, one-dimensional array 1.1 What is a one-dimensional array

A one-dimensional array is a storage space that is continuously allocated in memory.

1.2 Three ways to declare one-dimensional arrays first: assigning values at the time of declaration

data type [] array variable name ={initial value 1, initial value 2, initial value 3, ...};

The second type: direct declaration does not assign a value

data type [] Data variable name = new data type [array length];

The Third kind: from the second evolution, (not recommended in this way, the second method is recommended)

Data type data variable name [] =new data type [array length];

2, two-dimensional array 2.1 What is a two-dimensional array

Two-dimensional data is data that uses a one-dimensional array, whose elements are one-dimensional arrays.

2.2 Three ways to declare two-dimensional arrays first: assigning values directly when declaring

data type [] Data variable name = {{Initial value 11, initial value 12, initial value,......},{initial value of 21, initial value of 22, initial value,......},{initial value 31, initial value 32, initial value of,......},......};

The second type: direct declaration does not assign a value

data type [] Data variable name =new data type [number of rows] [number of columns];

For example: int [] [] arr=new int [3][2];//creates a 3-row, 2-column two-dimensional array

The third type: Declare the row first, then declare the column

data type [] Data variable name = new data type [number of rows] [];

Data variable name [number of rows] = new data type [number of columns];

For example:

int [] [] arr= new int[3][];//creates a two-dimensional array, specified as 3 rows

Arr[0]=new int[2];//The first row is specified as 2 columns

Arr[1]=new int[3];//The second row is specified as 3 columns

Arr[2]=new int[4];//The third row is specified as 4 columns

2.3 Examples of using two-dimensional arrays

Let's take a look at a code that traverses a two-dimensional array:

        int[] arr=New int[4] [];//declares a two-dimensional array, specified as 4 rowsarr[0]=New int[2];//line 1th, declared as 2 columnsarr[1]=New int[3];//Line 2nd, declared as 3 columnsarr[2]=New int[4];//Line 3rd, declared as 4 columnsarr[3]=New int[5];//Line 4th, declared as 5 columns         for(introw=0;row<arr.length;row++) {//Arr.length represents the total number of rows             for(intcolumn=0;column<arr[row].length;column++) {//Arr[row] represents line row+1, so arr[row].length represents the number of columns of row rowsSystem.out.print (Arr[row][column]);        } System.out.println (); }

From which we can see:

Arr-Represents a two-dimensional array of arr

Arr[n]--Represents the line n+1 of a two-dimensional array, which is 1 one-dimensional arrays

ARR[N][M]--Represents a specific element in a two-dimensional array

3, the allocation of the array in memory space

An array is a contiguous memory space allocated in memory, and the array name represents the address of the container.

A two-dimensional array is a one-dimensional array of one-dimensional arrays, so the representation of a two-dimensional array in memory should be similar to one-dimensional arrays:

A two-dimensional array of representations of the above graphs can be declared in the following format:

int [] [] arrays={{11,12,13},{14,15},{16}};

That is, a total of three rows, the first row three data, the second row two data, the third row a data.

4, the default initial value of various data types after declaration is complete

After I have used the array declaration in Java (for example: int [] arr=new int[3];), the default value is automatically assigned. The data types are divided into two main classes, the basic data type and the reference data type.

One of the basic data types:

The default array for Byte/short/int/long is 0

The default value for Float/double is 0.0

The default value for char is a space

The default value of Boolean is False

It is important to note that the default value of the char array is a space instead of 0, because the decimal 0 exactly corresponds to the null character in ASCII .

5, parse the length property in the array 5.1 the array in Java is an object?

The first thing to be sure is that the array in Java is an object, see the array in Java as an object

5.2 The class where the array object was created

We use the following procedure to find the class of an array object:

     int New int [Ten];       = A.getclass ();      System.out.println (Clazz.getdeclaredfields (). length);      System.out.println (Clazz.getdeclaredmethods (). length);      System.out.println (Clazz.getdeclaredconstructors (). length);      System.out.println (Clazz.getdeclaredannotations (). length);      System.out.println (Clazz.getdeclaredclasses (). length);      System.out.println (Clazz.getsuperclass ());  
System.out.println (clazz.getname ());

The output of the above code is:

00000class Java.lang.Object
[I

From the above code, we can see that the array of one-dimensional array class has no attributes, no methods, no construction methods, no comments, one-dimensional array directly inherit from Java.lang.Object, one-dimensional array of the class name is [I (actually a [character represents the name of a one-dimensional array, two [represents the name of a two-dimensional array, Three [characters represent the name of a three-dimensional array, ... )。

5.3 The source of the. Length property in the Java array.

This is strange, since the array class has no properties and methods, then why can call. length without error, after reviewing the JVM technical documentation in 5.3.3. Creating Array Classes The creation of the data class from the source:

"The Java Virtual machine creates a new array class with the indicated component type and number of dimensions", roughly meaning "Jav A virtual opportunity the corresponding array class is created according to the element type and dimension. ”。

The JVM does not put array classes in any package, nor does it give them a valid identifier name, presumably to avoid conflicts with JDK, third-party, and user-defined classes.

Now we know the source of the array class, but why the array class doesn't have any properties and methods, but it can be called. Length does not make an error, then let's take a look at a simple file's bytecode file:

 Public class Main {      publicstaticvoid  main (string[] args) {      int New int [2];           int i = a.length;      }  }

Open with Jclasslib we can draw:

0 iconst_2//Press int constant 2 into the operand stack1 NewArray 10 (int)//the 2 pop-up operand stack, as the length, creates an array of element type int, dimension 1, and pushes the reference of the array into the operand stack3 Astore_1//pops a reference to an array from the operand stack, saved in a local variable indexed to 1 (that is, a)4 Aload_1//pushes a local variable indexed to 1 (that is, a) into the operand stack5 Arraylength//pops the array reference from the operand stack (that is, a) and gets its length (the JVM is responsible for how to obtain it) and presses the length into the operand stack6 istore_2//pops the array length from the operand stack and stores it in a local variable indexed to 2 (that is, i)7return                    //The Main method returns

It can be seen that in this byte code, the length of the member variable is not seen at all, getting the array lengths is implemented by a specific instruction Arraylength. The compiler made a special processing of the syntax such as Array.Length and compiled it directly into the arraylength directive. In addition, the JVM creates an array class, which should be triggered by the NewArray directive.

6, reference article

1, in-depth analysis of length and length in Java ()

Properties of arrays in 2,java

3, why use length to get the lengths of the data

"Java" resolves an array in Java

Related Article

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.