Deep understanding of Java Virtual Machine notes---Class file magic number, version, constant pool

Source: Internet
Author: User
Tags field table

Magic number
The first 4 bytes of each class file are called Magic Numbers, and the value is 0xCAFEBASE, and its sole purpose is to determine if the file is a class file that can be accepted by the virtual machine. Using magic numbers instead of extensions is primarily a security-based consideration because the file extension can be arbitrarily altered.

Version number
The 4 bytes of the magic are then stored in the version number of the class file: the 5th and 6th bytes are the minor versions (Minor version), and the 7th and 8th bytes are the major version numbers (Major version). Java version is starting from 45, JDK1.1 after the major version of each JDK released on the major version number plus 1 (jdk1.0-1.1 used the 45.0-45.3 version number), the higher version of the JDK can be backwards compatible with the previous version of the class file, However, you cannot run a later version of the class file, even if the file format has not changed. JDK1.2 corresponds to the major version number 46,jdk1.3 is 47, and so on.

Constant pool
Following the major and minor version number is the constant pool entrance, the constant pool is the class file structure with the most associated with other items of the data type, is the largest data item occupies the class file space, and it is the class file in the first occurrence of the table type data item.
Since constant data in constant pools is not fixed, it is necessary to place a U2 type of data at the entrance of the constant pool, representing the constant pool capacity calculation value (Constant_pool_count). Unlike the Java language habit, this capacity count starts at 1 instead of 0. The purpose of the No. 0 constant is to satisfy some of the subsequent data pointing to the index value of the constant pool, which in particular cases requires the expression "do not refer to any one of the constant pool items" means. The capacity count of only the constant pool in the class file structure starts at 1, and for other collection types, including the interface index collection, the Field table collection, and the capacity calculation for the method table collection starts from 0.
There are two major types of constants in a constant pool: literal (Literal) and symbolic (symbolic References). Literal comparisons are similar to the concept of constants at the Java language level, such as literal strings, which are declared as final, constant values, and so on. The concept of symbolic reference is the principle of attribute compilation, which contains the following three types of constants:
A. Fully qualified name of class and interface (Fully qualified name)
B. Name and descriptor of the field (descriptor)
C. Name and descriptor of the method

Every constant in the constant pool is a table, there are 11 different structure of the table structure data, the 11 kinds of tables have a common feature, that is, the first bit of the table is a U1 type of the flag bit, representing the current constant attribute which constant type, 11 constant type specific meanings are as follows:

Each constant entry structure:



Deep understanding of Java Virtual Machine notes---Class file magic number, version, constant pool

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.