From the beginning of this section, we will officially go in the data structure of the world, then what is the first one, that is our array.
When I wanted to write the array, my first impression is to see its source code, unfortunately, by an array of very special, and looking for a long time, I did not find its source, with such thinking, I started mining in Java array. Wow, really fragrant!
First, the introduction of an array in Java
Is a simple array of complex data type, which is an ordered set of data, each element of the array have the same data type can be used and a uniform array name different subscripts to uniquely identify elements in the array. According to the dimensions of the array can be divided into one-dimensional arrays, two-dimensional arrays and multidimensional arrays and so on. Important to note that the array can only store the same data type (Object type except arrays).
Second, the array is a reference type?
Give the answer, yes, no doubt.
Note that the array is a data type, which itself is a reference type.
An array is a fixed-size data structure, all operations on the linear table can be achieved by an array. Although after the array is created, its size can not be changed, but when the store is no longer a linear array of new elements in the table, we can create a new large array to replace the current array. Such arrays can be implemented using dynamic data structures.
How to verify?
Define an array and found that it has all the methods of the Object class.
According to this example, we have in fact seen, all methods have superclass Object array, indicating that he is a class. And his own clone () method and the length property.
Third, how to understand the underlying array implementation
Since all methods have an array of Object, that if we could look at the source code of the array, the array to achieve the look of it?
Unfortunately, an array of very special, and his realization of a virtual machine compile time dynamically generated, so we can not directly view the source code, only to find out through the bytecode class after viewing compiled.
The JVM array object is a special object, the virtual machine can not confirm the size of the array from the metadata in the array, it Object Header for more than a word length of the memory array than ordinary objects, length byte compiled into a corresponding code reads this field on it.
I define basic data types and reference types to look at the final bytecode generated any difference.
Corresponding to the byte code is:
Note: After define and initialize an array in memory is allocated two spaces, one for the reference variable storage array for storing the other array itself.
When the program is developed to run deep underlying mechanism.
When viewed in an array, the array must take as two parts: one is an array reference, which is referenced in an array variable defined in the code; some of it is actually an array of objects, which is part of the memory operation, usually can not directly access it, you can only be accessed by an array of reference variables.
Four, Array's domain correlation length
In a lot of information are written, Array
in a similar public final int length
member variable. But in the "Java Language Specifications" 10.1. Array Types
clearly written, length is not part of the type;
-
An array's length is not part of its type.
You can see length
is not Array
a member variable.
Five, Java language specification definition of the Array
An array is a special type in Java, the object is different from the normal "instance of the class," the.
10.1. Array Types
10.8. Class Objects for Arrays
Every array has an associated Class object, shared with all other arrays with the same component type.Although an array type is not a class, the Class object of every array acts as if:
- The direct superclass of every array type is Object.
- Every array type implements the interfaces Cloneable and java.io.Serializable.
数组类型是由JVM从元素类型合成出来的。
10.7. Array Members
The members of an array type are all of the following:
- The public final field length, which contains the number of components of the array. length may be positive or zero.
从Java语言到Class文件,Java源码编译器会识别出对数组类型的length字段的访问,并生成对应的字节码。
以OpenJDK8的javac为例:
jdk8u/jdk8u/langtools: 84eb51777733 src/share/classes/com/sun/tools/javac/jvm/Gen.java
六、数组总结
在看数组的时候,因为class是动态创建的,所以看了很久,但是根据数组的特性,基本可以认为数组的域和方法,类似于:
- 数组可以是一维数组、二维数组或多维数组。
- 数值数组元素的默认值为 0,而引用元素的默认值为 null。
- 交错数组是数组的数组,因此,它的元素是引用类型,初始化为 null。交错数组元素的维度和大小可以不同。
- 数组的索引从 0 开始,如果数组有 n 个元素,那么数组的索引是从 0 到(n-1)。
- 数组元素可以是任何类型,包括数组类型。
- 数组类型是从抽象基类 Array 派生的引用类型。
参考文章:
- https://www.cnblogs.com/ysocean/p/7894448.html
- https://www.runoob.com/java/java-array.html
- https://chaxiaoniu.oschina.io/2017/08/07/JavaArrayOfDataStructure/
- https://blog.csdn.net/weixin_40617102/article/details/90264102
- https://www.zhihu.com/question/29649110
- http://c.biancheng.net/view/906.html
- https://blog.51cto.com/14028890/2384977