How to understand the length of the array?

xiaoxiao2021-03-06  35

The array in Java is actually an object, but it is indeed a special object. It is really very special.

So we don't make much object processing.

1. Array is an object, inheriting from Object

The only member variable that is more than Object, and it should be public.

2.java.lang.reflect.Array is final, so the array is definitely not its subclass.

This class is used to dynamically generate an array or an operator array (obtain length, etc.).

3.java.util.arrays is an array operating application class, mainly sort, fill, and separate findings.

Note: Sorting is rapid sorting, time complexity is O (n * log (n)), if you want to sort array

Arrays is definitely preferred.

4. The array has no corresponding class file, and String corresponds to string.class. But the array is not, and their

The name is very quirky, you can get new int [2] .getClass (). Getname ();

This is the biggest difference between other objects because the array class is generated at runtime.

5. What is the array of Length?

Step 1: We determine that the parent class of the array is Object,

New Object [0] .getclass (). getsuperclass () is Object.class

Obviously Object doesn't have a Length variable, so it should be a member variable in an array.

Step 2: New Object [0] .getClass (). The length of the array length returned at getDeclaredfields ()

For 0, here we naturally actively active this array of Class, but the results have proved that this Class is not

For Length's definition, this completely and the first step is violated.

Step 3: It is clear that the first step and the second step is to contradict, but the first step is not wrong.

I can't know what is going on, there are two possibilities here, I compare the results of the second step.

The array class does not have the Length member variable at all, so that the result of the second step is the same.

For Object [] Objs = new object [2];

This array object is obviously different from the general object, and the general constructor is not this.

For int i = objs.length;

Because I guess is that the array class is there is no longer member, but here is clearly called, I think

Here is the compiler to process, he will convert the length of the length of the length of the length to a method when compiling.

Call, such as Array.getLength (OBJ); although I tried to compile into a class file with these two methods

After getting different JVM instructions with Javap, I still feel that it is the array of compilers.

If I am wrong, who can help me explain the second step!

转载请注明原文地址:https://www.9cbs.com/read-65799.html

New Post(0)