Java Byte Code Summary

Source: Internet
Author: User

Reference
    • JAVAP Basic Use Method
    • Deep understanding of Java bytecode
    • From Java code to byte code
    • Java bytecode. class file Case Study
Byte Code core concept

The class file is a 8-bit byte stream, aligned in bytes. The bytecode is called because each instruction occupies only one byte. All opcode and operands are in byte-aligned order.

Data

Defined in the Java Virtual Machine specification. The class file format is stored in a pseudo-structure of a similar C-language struct, with only two data types

Unsigned number (base data type)

It is mainly used to describe the string composed of narrative number, index reference, quantity value, or UTF-8 encoding;
u1–1 bytes
U2–2 bytes
U4–4 bytes
U8–8 bytes

Table (composite data type)

Used to describe the data of a composite structure with hierarchical relationships.
Habitually ending with "_info"

class file format
Data Type name Quantity
U4 Magic 1
U2 Minor_version 1
U2 Major_version 1
U2 Constant_pool_count 1
Cp_info Constant_pool Constant_pool_count + 1
U2 Access_flags 1
U2 This_class 1
U2 Super_class 1
U2 Interfaces_count 1
U2 Interfaces Interfaces_count
U2 Fields_count 1
Field_info Fields Fields_count
U2 Methods_count 1
Method_info Methods Methods_count
U2 Attributes_count 1
Attribute_info Attributes Attributes_count
Class description
    • The first 4 bytes of the Magic:class file, and its sole purpose is to verify that the file is a class file that can be accepted by the JVM
    • Minor_version: The 5th and 6th bytes are the minor version number
    • Major_version: The 7th and 8th bytes are the major version number

    • Constant_pool_count: Constant Pool Size

    • Cp_info: Composite data structure, which is a set of constants, with 11 constant data structures

    • Access_flags: Access sign, access_flags is calculated as: Access_flags = Flaga | FLAGB | FLAGB ...

    • Field_info: The Field table (Field_info) is used to describe variables declared in a narrative class or interface, including class variables, instance variables, but not local variables and block variables within a method. Not the same as the Cp_info section. Cp_info There are 11 types of data structures because of the constant type, but there is only one field_info structure.

      (data structures such as the following)

      field access flags. and class of access to the flag, because the modifier slightly different, specifically see.


    • Method_info: The structure of the Method table collection (Method_info) and the Field table collection is consistent, only the access flags are different.


    • Attribute_info: Used to carry a collection of their own property sheets in class files, field tables, method tables, to describe proprietary information describing certain scenarios.

      The code inside the Java program method body is processed by the Javac compiler and the bytecode is finally stored in the Codes property. The code attribute does not exist for methods in an abstract class or interface.

    • The Sourcefile:sourcefile property primarily records the source name of the class file that is generated, and is also an optional attribute that can be turned off or required to generate this information using the Javac-g:none or-g:source option.

    • The localvariabletable:localvariabletable attribute is used to describe the relationship between variables in a local variable table in a narrative stack frame and variables defined by Java source code, but such a relationship is not required at execution time and is not generated by default into a class file. This information can be canceled or generated by using the-g:none or-g:vars option in Javac.

    • The linenumbertable:linenumbertable attribute is used to describe the corresponding relationship between the Java source line number and the byte code line number (the byte Code offset), which is not a required property at execution time. However, it is generated in the class file by default.

      You can also use the-g:none or-g:lines option in Javac to cancel or display this part of the information generated.

For details, see Java Bytecode. class file Case Study

On JVM execution

The Java Virtual machine (JVM) is based on the stack structure. For all the method calls produced by the original Main method, a frame is generated in the stack. Each of these frames includes a set of local variables, which are all the variables required for this method to execute, including a reference to this, a full argument to the method, and other locally defined variables. For a class method (that is, the static method), the list of references starts at 0. And for instance methods. Location 0 is used to store this reference.
Class execution is the JVM's sequential execution of instructions stored in code, such as the following:

Dynamic processes, such as the following:

publicclass Demo {    publicstaticvoidfoo() {       int1;       int2;       int5;    }}

Specific references
-Deep understanding of Java bytecode
-from Java code to byte code

JAVAP Disassembly

JAVAP is the disassembler that comes with the JDK. Ability to see the bytecode generated by the Java compiler for us. Through it, we can compare the source code and bytecode. This allows you to understand how much work is done inside the compiler.
Grammar:
JAVAP [command options] class ...
The JAVAP command is used to parse the class file. Its output depends on the options used.

If no option is used. JAVAP will output the public domain and method of the class passed to it. The JAVAP outputs it to the standard output device.
Command options
-help The Help information for the output JAVAP.
-L output rows and local variable tables.


-public only shows the public class and members.


-protected only shows the protected and public classes and members.
-package only shows packages, protected, and public classes and members. This is the default setting.
-private displays all classes and members.


-S output internal type signature.
Non-resolved code for each method in the-C output class. The instruction that constitutes the Java bytecode.
-verbose output stack size, locals and args for each method, and the compiled version number of the class file
-classpath[Path] Specifies the path that JAVAP uses to find the class. Assuming this option is set, it overrides the default value or CLASSPATH environment variable. Folders are separated by colons.


-bootclasspath[Path] Specifies the path used to load the bootstrap class.

By default, the Bootstrap class is the class that implements the core Java platform. Located below Jrelib.
-extdirs[dirs] Overrides the location of the search installation mode extension.

The default location for the extension is jrelibext.

Java Byte Code Summary

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.