(3) In-depth understanding of Java class file format (ii)

Source: Internet
Author: User
Tags uppercase character uppercase letter

Good text reprint: http://blog.csdn.net/zhangjg_blog/article/details/21487287

In an in-depth understanding of the Java class file format (a), this paper introduces the position and function of class file in the whole Java architecture, explains the whole format of class file, introduces the related content of magic number and version number, and summarizes the constant pool. In this article, you continue to describe the other content in the class file.

a special string in the class file first of all, the so-called special string appears in the constant pool in the class file, so in the previous blog, just about Chang introduced a general description. In line with the principle of gradual and reduced span, first the special string in class file to do a detailed introduction, and then go back to continue to explain the constant pool.  In the above, we mentioned that the special string is part of the symbol reference in the constant pool, and the concept of the symbolic reference will be mentioned later. Now we'll focus on the special string. Special strings include three types: the fully qualified name of the class, the descriptor of the field and method, and the method name of the special method. Let's introduce these three special strings separately.  (1) The fully qualified name of the class in a constant pool, the name of a type is not what we see in the source file, nor is it the package name plus the class name that we use in the source file. The fully qualified name in the source file is not the same concept as the fully qualifying name in the class file. The new naming in the source file is the package name plus the class name, between each part of the package name, between the package name and the class name, using the dot number to split. such as the Object class, the fully qualified name in the source file is Java.lang.Object. The fully qualified name in the class file is the replacement of the dot number with "/". For example, the fully qualified name of the Object class in the class file is Java/lang/object. If the reader has not previously contacted the class file format, is a beginner of the class file format, here do not need to know the fully qualified name in the class file is how to use, only need to know that the source file in the name of a class, in the class file is a fully qualified name expressed.  (2) Descriptor We know that there can be several fields and methods in a class, and how these fields and methods are expressed in the source file are familiar to us. Now that we're going to learn the class file format, we're going to ask, how does a field or a method be expressed in a class file? In this article, we'll discuss the description of methods and fields in the class file. The descriptors for methods and fields do not describe all the information for the method and the field, after all, the descriptor is just a simple string.  before explaining the descriptor, let's start by stating that all types have corresponding characters or strings in the descriptor. For example, each basic data type has an uppercase letter corresponding to it, and void has a large uppercase character to correspond to. The following table is the correspondence between void and the base data type in the descriptor.  
Basic data types and void types The corresponding character of the type
Byte B
Char C
Double D
Float F
Int I
Long J
Short S
Boolean Z
void V
 Basically, it corresponds to the first character of the type, where long and Boolean are special cases, the corresponding character of the long type in the descriptor is J, and the corresponding character of the Boolean type in the descriptor is Z.  both the base type and void have one uppercase character in the descriptor and they correspond, so how does the reference type (class and interface, enumeration) correspond in the descriptor? The corresponding string for the reference type (note that the reference type corresponds to a string in the descriptor), the format of the string is: 
    1. "L" + the fully qualified name of the type + ";"
 Note that there are no spaces between the three sections, which are tightly arranged.  The corresponding string such as Object in the descriptor is: ljava/lang/object;  The corresponding string in the ArrayList descriptor is: ljava/lang/arraylist; The corresponding string in the descriptor for the custom type Com.example.Person is: Lcom/example/person;.  we know that in the Java language An array is also a type, and the element type of an array and his dimension determine his type. For example, in int[] a declaration, the type of variable A is int[], in int[][] B declaration, the type of variable B is int[][], in object[] C declaration, the type of variable C is object[]. Since the array is a type, there should also be a corresponding string of the array type in the descriptor. In the descriptor of the class file, each dimension in the array's type is represented by a [representation, the corresponding string of the entire type of the array type in the following format:
    1. A number of "[" + string corresponding to the element type in the array
 Here's a name for example. The corresponding string for the int[] type is: [I. The corresponding string for the int[][] type is: [[I]. The corresponding string for the object[] type is: [Ljava/lang/object;. The corresponding string for the object[][][] type is: [[[Ljava/lang/object;.  after describing the corresponding string for each type in the descriptor, the following begins the description of the field and the method's descriptor.  A field descriptor is a character or string that corresponds to the type of the field. For example: int i, the descriptor of field I is I.  In Object O, the descriptor of the field O is Ljava/lang/object; 。 Double[][] D, the Descriptor for field D is [[D].  the descriptor of the method is more complex, including the type list of all parameters and the method return value. The format of this is: 
    1. (parameter 1 type parameter 2 type parameter 3 type ...) return value type
 where the type of the parameter or the return value type is represented by the corresponding character and the corresponding string, and the argument list is enclosed in parentheses, there are no spaces between the argument types, and there are no spaces between the argument list and the return value type.  The following is an example (this form is derived from the "Deep Java Virtual Machine").  
Method descriptor Method declaration
() I int GetSize ()
() ljava/lang/string; String toString ()
([ljava/lang/string;) V void Main (string[] args)
() V void Wait ()
(JI) V void Wait (long timeout, int nanos)
(ZILJAVA/LANG/STRING;II) Z Boolean Regionmatches (boolean ignoreCase, int tooffset, String other, int ooffset, int len)
([BII) I int read (byte[] b, int off, int len)
() [[Ljava/lang/object; Object[][] Getobjectarray ()

  (3) method name of the special method The first thing to make clear is that the special method here is to refer to the constructor method and type initialization method of the class. The construction method does not need to say more, as to the type initialization method, corresponds to the source code is static initialization block. That is, the static initialization block, in the class file is expressed in a method, this method also has the method descriptor and the method name.  The method name of the constructor method of a class uses string <init> representation, while the method name of the static initialization method is represented by string <clinit>. In addition to these two special methods, the method name of the other common method is the same as the method name in the source file. Summary So far, the special strings have been explained. Finally, make a summary: a special string in a class file includes the fully qualified name of the class (including interfaces, enumerations), the descriptor of the field, and the descriptor of the method. The fully qualified name of the class is simple and easy to understand, and the descriptors of the fields and methods may be slightly more complex because of the mappings involved in each type. To understand descriptors, it is important to memorize the descriptive characters or strings that correspond to each type (including 8 basic data types, class types, array types, and void) in the descriptor.  It is also important to note that the method and field descriptors do not include field names and method names, only field types are included in the field descriptor, and only parameter lists and return value types are included in the method descriptor.

(3) in-depth understanding of Java class file format (ii)

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.