Advanced programmers lessons - Architect of the road (2) - Array

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.

    public void test08(){ Object[] o = new String[11]; o[0]="1aaa"; int i=o.length; Integer[] a=new Integer[11]; a[0]=100; int j=a.length; int[] b=new int[11]; b[0]=100; int k=b.length; }

Corresponding to the byte code is:

 0 bipush 11
 2 anewarray #12 <java/lang/String>  //anewarray代表对象数组 5 astore_1 6 aload_1 7 iconst_0 8 ldc #25 <1aaa> 10 aastore 11 aload_1 12 arraylength //arraylength代表长度 13 istore_2 14 bipush 11 16 anewarray #26 <java/lang/Integer> //anewarray代表包装类数组 19 astore_3 20 aload_3 21 iconst_0 22 bipush 100 24 invokestatic #27 <java/lang/Integer.valueOf> 27 aastore 28 aload_3 29 arraylength 30 istore 4 32 bipush 11 34 newarray 10 (int) //newarray代表基本数组类型数组 36 astore 5 38 aload 5 40 iconst_0 41 bipush 100 43 iastore 44 aload 5 46 arraylength 47 istore 6 49 return

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, Arrayin a similar public final int lengthmember variable. But in the "Java Language Specifications" 10.1. Array Typesclearly written, length is not part of the type;

  • An array's length is not part of its type.

private static void test06() { String[] s = new String[2]; System.out.println(s.length); System.out.println(s.getClass().getDeclaredFields().length); try { System.out.println(s.getClass().getDeclaredField("length")); } catch (NoSuchFieldException e) { System.out.println(e.toString()); } }
打印:20java.lang.NoSuchFieldException: length

You can see lengthis not Arraya 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:

  1. The direct superclass of every array type is Object.
  2. 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:

  1. 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

base.load();
if (sym == syms.lengthVar) {
   code.emitop0(arraylength);
   result = items.makeStackItem(syms.intType);
}

六、数组总结

在看数组的时候,因为class是动态创建的,所以看了很久,但是根据数组的特性,基本可以认为数组的域和方法,类似于:

class A<T> implements Cloneable, java.io.Serializable { public final int length = X; public T[] clone() { try { return (T[]) super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e.getMessage()); } } }
  • 数组可以是一维数组、二维数组或多维数组。
  • 数值数组元素的默认值为 0,而引用元素的默认值为 null。
  • 交错数组是数组的数组,因此,它的元素是引用类型,初始化为 null。交错数组元素的维度和大小可以不同。
  • 数组的索引从 0 开始,如果数组有 n 个元素,那么数组的索引是从 0 到(n-1)。
  • 数组元素可以是任何类型,包括数组类型。
  • 数组类型是从抽象基类 Array 派生的引用类型。

参考文章:

  1. https://www.cnblogs.com/ysocean/p/7894448.html
  2. https://www.runoob.com/java/java-array.html
  3. https://chaxiaoniu.oschina.io/2017/08/07/JavaArrayOfDataStructure/
  4. https://blog.csdn.net/weixin_40617102/article/details/90264102
  5. https://www.zhihu.com/question/29649110
  6. http://c.biancheng.net/view/906.html
  7. https://blog.51cto.com/14028890/2384977

Guess you like

Origin www.cnblogs.com/anymk/p/11355853.html