I remember vamcily asked me: "Why is the length of the array obtained in the form of a member variable in. Length () and the length of the string obtained in the form of a member method )?"
When I heard it, I thought it was quite reasonable. Why are two different styles used to do the same thing? Besides, arrays in Java are actually complete (full-fledged) objects. Exposing member variables directly may not be a very oo style. So why did the talented people who designed Java do this?
With this question, I have read some materials, mainly about "How does JVM process arrays.
What is an array object class?
Since arrays are all objects, what are the classes of arrays? Of course it's not Java. util. arrays! Let's take the int one-dimensional array as an example to check whether it is.
Java code
public class Main { public static void main(String args[]){ int a[] = new int[10]; Class clazz = a.getClass(); System.out.println(clazz.getName()); }}
Run the above Code on Sun JDK 1.6 and the output is:
[I
It seems that the array class is very strange, not only does not belong to any package, but the name is not a legal identifier (identifier ). For specific naming rules [1], see javadoc of Java. Lang. Class. getname. In short, the array class name consists of several internal names of '[' and array element types. The number of '[' represents the array dimension.
An array with the same type element and dimension belongs to the same class. If the two arrays have the same element type but different dimensions, they also belong to different classes. If the element types and dimensions of the two arrays are the same but the length is different, they still belong to the same class.
What are the members of the array class?
Now that we know what the Class Name of the array is, let's look at what the class of the array is like? What member variables are there? What member methods are available? Where is the length member variable? Is there no member Method for length?
Find it. The JDK Code does not contain the '[I' class. Think about it. '[I' is not a legal identifier, and Java code such as public class [I {...} will certainly not appear. For the time being, no matter who declares the Class I or how it is declared, first use the reflection mechanism to find out.
Java code
public class Main { public static void main(String[] args) {int a[] = new int[10];Class clazz = a.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);System.out.println(clazz.getSuperclass());}}
Run the above Code on Sun JDK 1.6 and the output is:
0
0
0
0
0
Class java. Lang. Object
It can be seen that [I is a direct subclass of Java. Lang. Object and does not declare any member variables, member methods, constructors, or annotation. It can be said that [I is an empty class. We can immediately think of a question: how can we not even have the length member variable? If not, how can the compiler not report syntax errors? The compiler must have specially processed array. length!
Where is the array class declared?
No matter why there is no length member variable, let's first figure out [Where is I declared. Since [I is not a legal identifier, this class must be explicitly declared in Java code. The JVM can only generate it at runtime. The JVM generation class is still very easy. You don't even need to generate bytecode and directly create type data in the Method Area, which is almost complete.
I have no strength to view the JVM source code, so I flipped through the javatm virtual machine specification second edition, and it was verified. For more information, see 5.3.3.
Creating array classes.
The description is rigorous, and the content of the class loader and initialization class loader is also added. Skip this and briefly summarize:
- The Class Loader first checks whether the array class has been created. If no, it indicates that an array class needs to be created; If yes, it does not need to be created.
- If the array element is of reference type, the Class Loader first loads the class of the array element.
- JVM creates an array class based on the element type and dimension.
Well, the JVM guy secretly created the [I class. The JVM does not place the array class in any package, nor give them a valid identifier name. It is estimated that it is to avoid conflicts with JDK, third parties, and user-defined classes.
Think about it, the JVM must also dynamically generate an array class, because the number of Java array classes is related to the element type and dimension (up to 255). A considerable number of classes are not pre-declared.
There is no length member variable!
We have found that the lazy JVM does not generate the length member variable for the array class, so how can the syntax like array. length be compiled and executed?
Let's look at the bytecode! Write the simplest code and use jclasslib to view the bytecode.
Java code
public class Main { public static void main(String[] args) {int a[] = new int[2]; int i = a.length; }}
Use Sun JDK 1.6 to compile the above Code and use jclasslib to open the main. Class file to obtain the bytecode of the main method:
0 iconst_2 // press int type constant 2 into the operand Stack
1 newarray 10 (INT) // sets 2 to pop up the operand stack, as the length, create an array with the element type of int and dimension of 1, and press the array reference into the operand Stack
3 astore_1 // pop up the array reference from the operand stack and save it in the local variable (A) with index 1
4 aload_1 // press the local variable (I .e., a) with an index of 1 into the operand stack.
5 arraylength // The array reference (a) is popped up from the operand stack, and its length is obtained (JVM is responsible for how to obtain it), and the length is pushed to the operand stack.
6 istore_2 // The array length is popped up from the operand stack and saved in the local variable (I) with the index 2.
7 return // return the main method
It can be seen that in this bytecode, the length member variable is not seen at all. Obtaining the array length is implemented by a specific instruction arraylength (no matter how it is implemented, there is always a way for JVM ). The compiler performs special processing on the syntax such as array. Length and directly compiles it into the arraylength command. In addition, the JVM should be triggered by the newarray command to create an array class.
Naturally, the compiler can also perform special processing on the syntax such as array. Length () and directly compile it into the arraylength command. In this way, we can use the method call style to obtain the length of the array, which looks a little more oo. Why not use the array. Length () syntax? Maybe it was the genius who developed Java who had a preference for. length, or just threw a coin and shoot his head. Form is not important. What is important is that we understand the mechanisms behind it.
Array in Java
Finally, I would like to express my thoughts on the arrays of pure objects in Java.
Compared with arrays in C/C ++, Java arrays are much more secure. C/C ++ often encounters cache overflow or array access out-of-bounds issues, which do not exist in Java. Because Java uses specific commands to access array elements, these commands will check the length of the array. If an exception is found, java. Lang. arrayindexoutofboundsexception is thrown.
Java array elements are more flexible. An array can also be an array, as long as the array types of all elements are the same. We know that the array type and length are irrelevant, so the element can be an array of different lengths. In this way, the multi-dimensional array of Java is not necessarily a regular matrix.
From: jarfield blog (http://jarfield.iteye.com/blog/564721)