Java class file (.Class file) is a.Java source code has been compiled Java program is compiled into a carefully defined format that can be loaded and executed any JVM class file. Before being loaded with JVM, the class file may be caused by network transmission and.
class file is independent of the underlying platform, so it can be applied to more places. They consist of simple JVM byte code, so you can move forward with ease. Class files are often compressed at a high speed through the network, to the world of JVM.
What is the
class file in?
Java file contains the JVM need to know about all of a Java class or interface. According to their order of appearance, the main part is: the magic number (Magic), version number (version), constant pool (constant pool), access identifier (access flags), the current district area (this class) the super class area (super, class), father (interfaces), the interface area of field area (fields), the list of methods (Methods), district property area (attributes).
stored in the class file information often vary in length, so the actual length information to be loaded before cannot be predicted. For example, the number of methods in the method area, between classes is not the same, a number of methods depending on the source code defined in the. Class file, this information the actual size or length, are arranged in the information content before. So, when the JVM class file is loaded, the first variable length information is read. Once JVM knows the size of the information, it can correctly read the actual information content.
class file, usually no blank or filled between adjacent characters of the different information; everything in bytes (byte). This makes the boundary alignment class file is small, suitable for network transmission.
for JVM in the class loading file, know what information is needed and where they can obtain the required information, each class file part of the order is strictly defined. For example, each JVM all know that the first 8 bytes of the class file by the magic number and version number, starting from the ninth byte constant pool access identifier, followed by the constant pool in the area. However, because of the constant pool length is variable, before reading after the constant pool, JVM is not known access identifier from what specific place to start. Once read constant pool, JVM will know that 2 bytes is the next access identifier area.
(Magic) and version number (Version)
class files of 4 bytes are 0xCAFEBABE. the magic number for Java class files easier to identify, because the class files outside of the file is almost impossible to have this same four bytes at the beginning. It is called the magic number, because it can be file format designers out of the hat "(??). It is the only requirement for, cannot be realistic existing file format. According to one of the main occupation of the initial members of Java team Patrick Naughton said, before the name was" Java "as the Java language, the magic number of words have already been selected. We are looking for an interesting time. Digital.0xCAFEBABE is unique and easy to remember as nice as Peet 's Coffee said on behalf of the barista, predicts future name of the Java language, which is totally a coincidence.
class file next 4 bytes contain the version numbers (major version) and small version (minor version). These numbers identify specific class files using the class file format, so that JVM can verify whether the class file can be loaded. Each JVM has a it can load the maximum version number, refuse loading is greater than the maximum version number of the file.
(Constant Pool)constant pool
class file associated with the class or interface constants in the constant pool. Some constants in the constant pool can see is a string literal (literal strings), the value of the variable final (final variable values), class, interface, variable names and types of variables, the method name and signature method (method names and signatures). Methods the signature value type (return type) by the method of return and a set of parameter types (argument types).
constant pool is organized into a variable length array. Each element has a constant element in an array. In the whole class file, referenced by constants indicate their positions in the array index. The first integer constant index value is 1, second is 2, and so on. In front of the constant pool array the number of elements written in the constant pool, so in the class loading file, JVM know that it needs to be loaded with constant.
Each element of the
in the constant pool to indicate its type single byte tag (tag). When JVM sees this tag, you can know what next type constant. For example, if you see a string representing the label, JVM will consider the next 2 bytes is the length of the string, the string length is.and then "" bytes
in the remainder of this article, I sometimes say the constant pool array n elements by constant_pool[n]. From the constant pool like a tissue array, which is justified; but remember, these elements have different sizes and types, and the first element is 1.
(Access Flags)area identifier
2 bytes after the
constant pool is access identifier, it indicates that the file is a class or interface; the class or interface is open (public) or abstract (Abstract); if it is, the class is not final.
the next 2 bytes is the current class area, it is just a constant pool. The array index by the current class reference constant constant_pool[this_class], consists of two parts: single byte and double byte tag (tag) name index (name index). The label is equal to CONSTANT_Class, a representation of the elements contained in the class or interface information the value of.Constant_pool[name_index] is a class or interface name string constants.
class section slightly reveals how the constant pool is used. The area itself is only a constant pool index. When JVM find constant_pool[this_class], it found a label that is a CONSTANT_Class element.JVM know CONSTANT_Class elements in the label (tag), there is always a call. Index (name index) constant pool double byte index. Then it finds constant_pool[name_index], the string.contains a class or interface name
class is the superclass, the constant pool index.Constant_pool[super_class] is 2 bytes of the CONSTANT_Class elements, it points to the current super class.class inherits directly
interface region (Interfaces)
At the beginning of the 2 bytes of
interface area, said the definition file class (or interface) interface number. Then is an array, it contains the index of each interface realized in the constant pool in
Each interface is the CONSTANT_Class element in the constant pool, it points to the interface name.
field, starting with 2 bytes to represent the number of fields to the class or interface contains. The field is an instance variable, or class or interface class variables. The following is a variable length structure element array, a structure of a field. The related information of each structure contains a fields, such as field names, field type, if final is variable, including field value. Some of the information in the structure, the other part contains the location pointed to by the structure of the.index in the constant pool
this part only fields are defined directly in the class file in the class or interface declaration variables; inherited from the field super class or interface in.
The method of
method, 2 bytes to represent the class or interface method in number start. This number only contains the method defined in the current class explicitly, not inherited from the superclass method. After the number is.
said several methods related information including structure of each method, including the method descriptor (method descriptor, including the return type and parameter list), local variables need word stack method (stack words) the number of operand stack method (operand stack) the maximum stack words need the exception table acquisition method, method the logical sequence of byte codes and line number table.
property area (Attributes)
at the end of the property, it provides general information defined in the class file in a particular class or interface. The properties start with 2 byte number attribute, then the property itself. For example, a source attribute attribute that: it is said that the current class was compiled from the source file name.JVM will quietly ignore any.