Deep thinking about Java arrays

Source: Internet
Author: User
Array

People who just started touching Java arrays will hear a similar phrase: Java is a pure object-oriented language, and his array is an object. As a then, the author of an object in the way to use the array, peace of conscience. It was not until I reached the array of C that I realized how "unnatural" it was to use the array as a class to implement.

  
First we look at the surface phenomenon, the array is created with the following statement:

myclass[] arr = new MYCLASS[9];

The common class uses the following statement:

MyClass obj = new MyClass ();

That is, when you create an array, you do not use parentheses to pass the argument. This makes the array and the normal class look a lot different because the parameters in the parentheses are passed to the constructor method, which in turn makes the sense that the array class is not constructed.

  
Further deeper, there are many things that make people feel unnatural. To be sure, Java does treat arrays as a class. Or use the example above to illustrate:

You can get the class instance of myclass[] in the following ways: Arr.getclass () or Myclass[].class. In this way, I can "pry" into the array class.

  
Class clazz = Myclass[].class;
System.out.println (Clazz.getconstructors (). length);

  
The printed result is 0, proving that the array class does not have a construction method.

  
If you enforce clazz.newinstance (), you will get the following error.

Java.lang.InstantiationException: [Larraytest.myclass;

Proof that an array class cannot create an instance by ordinary reflection.

  
And look at the "truth" of the Array class:

System.out.println (Clazz);

The output is:

[Larraytest.myclass

A slight end to the Java class file structure is known, and this string means that an element type is arraytest. A one-dimensional array of MyClass. In other words, the array type is not the same as the ordinary class, with a fully qualified path name + class name as its own unique indicator, but rather with [+ one or more l+ array element class fully qualified path + class to be uniquely marked. This () is also the difference between an array and an ordinary class. The difference seems to be a way of stating that arrays and ordinary Java classes differ greatly in implementation. Because Java virtual machines (Java instruction sets) are sure to differentiate between array classes and ordinary classes. The author suspects that there may be specialized Java Virtual machine directives to handle arrays.

Now that we can get the class class instance of the array, we'll definitely need to call ClassLoader's defineclass (not necessarily LoadClass method) method to construct a class instance. The Java Virtual Machine specification stipulates that any class that can be loaded, if its class file is stored on a file system, can store only one class information, that is, the information of an array class cannot be stored on a local disk as a class file ( Otherwise, any class should have 255 array classes ... in that case, it means that the Java virtual machine must have a built-in piece of data to declare an array class, regardless of the number of arrays. This is compliant with the Java Virtual Machine specification, which stipulates that class class data can come from any medium, including local disks, networks, databases, memory, and so on.

Analysis to here, I can basically be sure: Java array of object-oriented operations support is a command-level, that is, Java virtual machines have specific instructions for the array. The class class instance of the array is dynamically loaded by the Java Virtual machine, and its structure is different from the class instance of the normal Java class.

There is a Java.lang.reflect.Array class in the JDK API, which provides a number of methods (most of which are native methods, which, in another respect, prove that support for Java arrays is supported by proprietary instructions, or what local methods do ^_^), To compensate for the limitations of our array operations.

The following sentence is used to create a one-dimensional, length 10, type arraytest. Array of MyClass:

Arraytest. Myclass[] arr = (arraytest. Myclass[]) array.newinstance (arraytest. MyClass, 10);

The following sentence is used to create a two-dimensional, 3 by 5, type Arraytest. Array of MyClass:

int[] Arrmodel = new int[]{3,5};
Object arrobj = array.newinstance (Sub.class, Arrmodel);

Of course you can use an array reference to point to the above two-dimensional array, where we point to him with an object reference.
When used, we can also use the method provided by the array class to implement:

System.out.println (Array.getlength (arrobj);//The first dimension length is 3
System.out.println (Array.getlength (Array.get (Arrobj, 2)))//The second dimension is 5, if 3 is written here, You'll get the java.lang.ArrayIndexOutOfBoundsException you intend.

The print result is as we think:

3
5

There are also some strange phenomena with the class class instance of an array:

When the code Java.lang.reflect.Field Fieldarr = Clazz.getfield ("Length") is run, an exception is thrown: Java.lang.NoSuchFieldException:length, This seems to say that the array class does not have the length of this field, which is actually the most we use (that is, the domain is definitely there). The author thinks about the class instance of the array, the realization of the array, and so on, there are many "fishy" in it.

By the way, Java arrays can be up to 255 dimensions. This lets a person see the shadow of C, hehe. "Java treats arrays as a Java class" It's easy to say, it's natural, but there are many things that are not easy to think about.



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.