Why do I use length to get the length of a Java array __java

Source: Internet
Author: User
Tags arrays naming convention

Remember vamcily once asked me: "Why get the length of an array with. Length (in the form of a member variable), and get the length of a string with. Length (in the form of a member method). ”

At that time, I thought it was reasonable to ask. To do the same thing, why use two different styles of style? Moreover, the array in Java is actually a complete (full-fledged) object, exposing the member variables directly, may not be a very OO style. So why did the Java-design genius do that?

With this in question, I looked at some of the data, mainly about how the JVM handles arrays.

What the class of the array object is.

Since arrays are objects, what exactly is the class of the array? Of course not java.util.Arrays. Let's take an int one-dimensional array for example and see what happens.

[Java] View plain copy public class Main { public static void Main (Str           ing 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

The class of the array looks strange, and not only does it not belong to any package, but the name is not a valid identifier (identifier). The specific naming convention [1] can see Javadoc of Java.lang.Class.getName (). Simply put, the class name of an array consists of several internal names of ' [' and array element types, and the number of ' [' represents the dimensions of the array.

An array of the same type elements and the same dimension, belonging to the same class. If the two arrays have the same element types, but the dimensions are different, they belong to different classes. If the element types and dimensions of the two arrays are the same, but the lengths are different, they are still in the same class.

What are the members of the array's class?

Now that we know what the class name of the array is, let's look at what the array class is like. What member variables are there. What member methods are available. Length is where the member variable is. Is there no length () This member method.

Look for it, in the JDK code does not find the "[I" class. Think about it, too, ' [I ' is not a valid identifier, and certainly no public class [I {...}] Such Java code. For the moment, let us look at the reflection mechanism first, regardless of who declares it or how it is declared.

[Java] View plain copy PublicclassMain { PublicStaticvoidMain (string[] args) {intA[] =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

As you can see, [I this class is a direct subclass of Java.lang.Object, without declaring any of its member variables, member methods, constructors, and annotation, so to speak, [I is an empty class.] We can immediately think of a question: how to even the length of the member variable does not have it. If it does not, the compiler does not report syntax errors. Presumably the compiler has done a special deal with Array.Length wow.

Where the class of the array is declared.

Regardless of why there is no length member variable, let's first figure out what the class is declared. Since [i is not a valid identifier, this class must be explicitly declared in Java code. To think about it, only the JVM itself was generated at runtime. JVM generation classes are also an easy thing to do, even without generating bytecode, creating type data directly in the method area, which is almost complete.

There is no strength to see the source code of the JVM, so turned over the JAVATM Virtual Machine specification Second Edition, really got the validation, the relevant content reference 5.3.3 creating Array Classes.

The description of the specification is rigorous, and the content that defines the class loader and initializes the class loader is also doped. Regardless of these, let's briefly summarize: the ClassLoader first looks at whether the array class has been created. If not, it means that you need to create an array class, and if so, you don't need to create it. If an array element is a reference type, the class loader first loads the class of the array element. The JVM creates the corresponding array class based on the element type and dimension.

Oh, it is the JVM this guy secretly created the [I class. The JVM does not put array classes in any packages, nor does it give them a valid identifier name, presumably to avoid conflicts with JDK, third parties, and user-defined classes.

Again, the JVM must dynamically generate an array class, because the number of Java array classes has to do with the element type, dimension (up to 255), which is quite a lot, and cannot be declared well in advance.

Actually there is no length for this member variable.

We have found that the lazy JVM does not generate the length of this member variable for the array class, so how does Array.Length's syntax get done by compiling it?

Let's take a look at the byte code. Write the simplest code and use Jclasslib to view the bytecode.

[Java] View plain copy public class Main { public static void Main (Str           Ing[] args) { int a[] = new int[2];       int i = a.length; }   }

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.