Basic Introduction: For more information about java array-Linux general technology-Linux programming and kernel, see the following. People who are new to java arrays will hear something similar: java is a pure object-oriented language, and its array is also an object.
As a result, I am comfortable using arrays as an object. It wasn't until I got in touch with the array of C that I found out how "unnatural" to use arrays as a class in implementation ".
First, let's take a look at the surface phenomenon. When creating an array, we use the following statement:
MyClass [] arr = new MyClass [9];
Common classes use the following statements:
MyClass obj = new MyClass ();
That is to say, parameters are not transmitted using parentheses when an array is created. This makes the array and the common class look a lot different, because the parameters in parentheses are passed to the constructor, which makes people feel that the array class has no constructor.
After thinking deeply, there are still a lot of things that make people feel unnatural. Java treats arrays as a class. Use the following example to describe:
You can use the following method to obtain the Class instance of MyClass:
Arr. getClass () or MyClass []. class. In this way, I can "snoop" into the array class.
It proves that the array class cannot create an instance through normal reflection.
Let's take a look at the "true colors" of the array class ":
System. out. println (clazz );
The output is:
[Larraytest. MyClass
You can understand the structure of the Java Class file with a slight knot. This string means a one-dimensional array with the element type of arraytest. MyClass. That is to say, the array type is not the same as that of a common class. It uses a fully qualified path name + class name as its unique identifier, it is uniquely identified by [+ one or more L + array element classes with fully qualified paths + classes. This () is also the difference between arrays and common classes. To some extent, this difference seems to indicate that there is a big difference between arrays and common java classes. This is because the java Virtual Machine (java Instruction Set) will certainly distinguish between array classes and common classes. I guess there may be special Java Virtual Machine commands to process arrays.
Now that we can get the Class instance of the array, it means we must call the defineClass (not necessarily the loadClass method) method of ClassLoader to construct a Class instance. The Java virtual machine specification specifies that any class that can be loaded, if its class files are stored in the file system, then *. the class file can store only one class information, that is, the information of the array class cannot be stored on the local disk as a class file (otherwise, any class will have 255 array classes ......), In this case, the Java Virtual Machine must have a piece of data (no matter how many arrays) that is used to declare the array class ). This is in line with Java Virtual Machine specifications. The specifications stipulate that class data can come from any media, including local disks, networks, databases, and memory.
After analysis, I can basically make sure that java supports the operations on Array objectization at the command level. That is to say, the java Virtual Machine has commands dedicated to arrays. An array Class instance is dynamically created and loaded by a java Virtual Machine. Its structure is somewhat different from that of a common java Class instance.
The jdk api has a java. lang. reflect. array class. This class provides many methods (most of which are native methods, which proves that java supports Arrays Using dedicated commands, otherwise, we will use the local method ^_^) to make up for our limitations on Array Operations.
The following statement is used to create an array of the arraytest. MyClass type with a one-dimensional length of 10:
The following statement is used to create an array of two-dimensional, 3 × 5, and type: arraytest. 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. Here we use an Object reference to point to it.
In use, we can also use the methods provided by the Array class to implement:
System. out. println (Array. getLength (arrObj); // the length of the first dimension is 3.
System. out. println (Array. getLength (Array. get (arrObj, 2); // The second-dimensional length is 5. If you write 3, you will get the expected
Java. lang. ArrayIndexOutOfBoundsException
The printed result is as I thought:
3
5
For array Class instances, there are some strange phenomena: Running code java. lang. reflect. field fieldarr = clazz. getField ("length"); will throw an exception: java. lang. noSuchFieldException: length, which seems to be saying that the array class does not have the length field, and this field is actually the most used one (that is, this field must exist ). I think there are many "tricks" about the array Class instance and array implementation.
By the way, java Arrays can only be 255 dimensions at most. This is the shadow of C.
"Java treats arrays as a java class." This is easy to say and can be used naturally. However, there are still a lot of things that are not simple to think about.
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