Oh, it is clear that the river is not so shallow as the ox Uncle said, and no small squirrel said so deep, only to try to know himself! Hearsay can only see the phenomenon, only to try it out, only to know its depth!!!!!
First, what is an array
array? What is an array? The arrays in my impression should look like this: create and assemble them with the New keyword, access its elements by using the reshape index value, and its dimensions are immutable!
But this is just the most superficial thing in the array! A little deeper? This is the case: the array is a simple composite data type, which is a collection of ordered data, each of which has the same data type, and we uniquely determine the elements in the array by the array name plus a subscript value that does not cross over.
And the deeper, that is, the array is a special object!! (Not very good for this LZ, not for the JVM, so there is limited insight). The following references: Http://developer.51cto.com/art/201001/176671.htm, http://www.blogjava.net/flysky19/articles/92763.html?opt=admin
No matter what an array is in another language, it is an object in Java. A more specific object.
public class Test {public static void Main (string[] args) { int[] array = new INT[10]; System.out.println ("The parent class of the array is:" + array.getclass (). Getsuperclass ()); SYSTEM.OUT.PRINTLN (the class name of the array is: "+ array.getclass (). GetName ());} } -------Output:array's parent class is: Class Java.lang.Objectarray is: [I
As can be seen from the above example, the array is the direct subclass of object, which belongs to the "First Class object", but it is very different from the ordinary Java object, from its class name can be seen: [I, what is this?? I did not find this class in the JDK, saying that "[I" is not a valid identifier.) How do you define it as a class? So I think the sum of those geniuses must have done something special with the bottom of the array.
Let's look at the following example:
public class Test {public static void Main (string[] args) { int[] array_00 = new INT[10]; System.out.println ("One-dimensional array:" + Array_00.getclass (). GetName ()); int[][] array_01 = new INT[10][10]; System.out.println ("Two-dimensional array:" + Array_01.getclass (). GetName ()); int[][][] array_02 = new INT[10][10][10]; System.out.println ("Three-dimensional array:" + Array_02.getclass (). GetName ());} } -----------------Output: One-dimensional array: [I two-dimensional array: [[i] three-dimensional arrays: [[i
By this example we know: [Represents the dimension of an array, one [represents one dimension, two [denotes two dimensions]. It is simple to say that the class name of an array consists of several ' [' and ' internal names ' and array element types. I don't know, we'll see:
public class Test {public static void Main (string[] args) { System.out.println ("object[]:" + object[].class); C2/>system.out.println ("object[][]:" + object[][].class); System.err.println ("object[][][]:" + object[][][].class); System.out.println ("Object:" + Object.class);} } ---------Output:object[]:class [Ljava.lang.object;object[][]:class [[[Ljava.lang.object;object[][][]:class] [[ Ljava.lang.object;object:class Java.lang.Object
From this example we can see the "truth" of the array. It can also be seen that the array and the ordinary Java class is different, the ordinary Java class is the fully qualified path name + class name as its own unique identifier, and the array is a number of [+l+ array element class fully qualified path + class to be the most unique indicator. This difference may, to some extent, indicate that there is a big difference in the implementation of arrays as well as common Java classes, which may be used to make the JVM differentiate between working with arrays and ordinary Java classes.
For the time being, whatever this [I is], who declares it, and how it is declared, I do not know now! But one thing to be sure: this is determined at runtime). Let's look at the following:
public class Test {public static void Main (string[] args) { int[] array = new INT[10]; Class clazz = Array.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); }} ----------------output:00000
From this running result can be seen, our dear [I no life any member variables, member methods, constructors, annotation even the length member variable this is not, it is a thorough bottom of the empty class. There is no declaration length, so when we array.length, how can the compiler not error? Indeed, the length of the array is a very special member variable. We know that the array is the direct kind of object, but the object is not the length of this member variable, then length should be the member variable of the array, but from the above example, we find that the array does not have any member variables at all, the two are not contradictory to each other?
public class Main {public static void Main (string[] args) { int a[] = new int[2]; int i = A.length; }}
Open the class file and get the byte code of the Main method:
0 iconst_2 //press int constant 2 into the operand stack 1 newarray (int) //2 eject the operand stack as length, create an array of element type int, dimension 1, and press the reference of the array into the operand stack 3 astore_1 //A reference to an array is popped from the operand stack and stored in a local variable indexed to 1 (that is, a) 4 aload_1 //The local variable indexed to 1 (that is, a) is pressed into the operand stack 5 Arraylength/ /Eject an array reference from the operand stack (that is, a) and get its length (the JVM is responsible for implementing how to get It) and press the length into the operand stack 6 istore_2 //To eject the array length from the operand stack, Save in a local variable indexed to 2 (that is, i) 7 return //main method returns
In this bytecode we still do not see the length of this member variable, but see this: arraylength, this instruction is used to get the length of the array, so the JVM is the length of the set of special processing, it is by arraylength this command to achieve.
Second, how to use the array
With a preliminary understanding of what an array is, here is a brief introduction to how arrays are used.
the use of arrays is nothing more than a four-step process: declares an array, allocate space, assignment, processing.
Declaring an array: Just tell the computer what type of array it is. There are two types: int[] array, int array[].
Allocate space: Tell the computer how much contiguous space it needs to allocate to the array, keeping in mind that it is contiguous. Array = new INT[10];
Assignment: The assignment is to put the data in the allocated space. Array[0] = 1, array[1] = 2 ... In fact, the allocation of space and assignment is done together, that is, to complete the initialization of the array. There are three different forms:
int a[] = new int[2]; The default is 0, if the reference data type is null int b[] = new int[] {1,2,3,4,5}; int c[] = {1,2,3,4,5};
Processing: Is the operation of an array element. The data is confirmed by the array name + a valid subscript.
PS: due to limited capacity, so "what is the array" is mainly referred to this post: http://developer.51cto.com/art/201001/176671.htm. The next article will cover some of the features of the array, such as efficiency issues, the use of arrays, shallow copies, and conversion problems with lists.
Java Enhancement Chapter (18) One of-----arrays: Understanding Java Arrays