Read deep understanding of Java Virtual Machine-Part 1: Structure of the Class file,

Source: Internet
Author: User
Tags field table

Read deep understanding of Java Virtual Machine-Part 1: Structure of the Class file,

1. Generate

Source code (. java file) --> Compiler (such as javac) --> bytecode (. class file) --> Virtual Machine (such as HotSpot) Execution

 

2. Class File

 1) composition:

 2) Example:

[1]. java File

Desktop/test/Father. java, desktop/test/Father_interface.java, desktop/test/Son. java;

[2] compile the. class File

Cd desktop/test;

Desktop/test> javac Son. java;

[3] viewing the. class File

Desktop/test> javap-verboseSon;

 

Classfile/C:/Users/Administrator/Desktop/test/Son. class Last modified 2016-4-11; size 1005 bytes MD5 checksum 7d138d76267a6bb372eba05296e2c932Magic numberCompiled from "Son. java" public class test. Son extends test. Father implements test. Father_interfaceIndex (class 1 index,2Parent index, 3 interface index,Point to constant pool)SourceFile: "Son. java" minor version: 0 major version: 51Motherboard IDFlags: ACC_PUBLIC, ACC_SUPERAccess flag <current class>Constant pool:Constant pool#1 = Methodref #16. #33 // test/Father. "<init>" :() VSymbol reference {#2 = Fieldref #15. #34 // test/Son. sId_literal: I #3 = Fieldref #35. #36 // java/lang/System. out: Ljava/io/PrintStream; #4 = Class #37 // java/lang/StringBuilder #5 = Methodref #4. #33 // java/lang/StringBuilder. "<init>" :() V #6 = String #38 // statical_sId = #7 = Methodref #4. #39 // java/lang/StringBuilder. append :( Ljava/lang/String;) Ljava/lang/StringBuilder; #8 = Fieldref #15. #40 // test/Son. statical_sI D: I #9 = Methodref #4. #41 // java/lang/StringBuilder. append :( I) Ljava/lang/StringBuilder; #10 = Methodref #4. #42 // java/lang/StringBuilder. toString :() Ljava/lang/String; #11 = Methodref #43. #44 // java/io/PrintStream. println :( Ljava/lang/String;) V #12 = String #45 // Son instance constructor! #13 = String #46 // hello! This is son! #14 = String #47 // Son class constructor <clinit> RUN! #15 = Class #48 // test/Son 1 #16 = Class #49 // test/Father 2 #17 = Class #50 // test/Father_interface 3} Symbol reference#18 = Utf8 final_sId #19 = Utf8 I #20 = Utf8 ConstantValue #21 = Integer 123Literal#22 = Utf8 statical_sId #23 = Utf8 sId_literal #24 = Utf8 sId_nonLiteral #25 = Utf8 <init> #26 = Utf8 () V #27 = Utf8 Code #28 = Utf8 LineNumberTable #29 = Utf8 sayHello #30 = Utf8 <clinit> #31 = Utf8 SourceFile #32 = Utf8 Son. javaSymbol reference {#33 = NameAndType #25: #26 // "<init>" :() V #34 = NameAndType #23: #19 // sId_literal: I #35 = Class #51 // java/lang/System #36 = NameAndType #52: #53 // out: Ljava/io/PrintStream; #37 = Utf8 java/lang/StringBuilder #38 = Utf8Statical_sId =#39 = NameAndType #54: #55 // append :( Ljava/lang/String;) Ljava/lang/StringBuilder; #40 = NameAndType #22: #19 // statical_sId: I #41 = NameAndType #54: #56 // append :( I) Ljava/lang/StringBuilder; #42 = NameAndType #57: #58 // toString :() ljava/lang/String; #43 = Class #59 // java/io/PrintStream #44 = NameAndType #60: #61 // println :( Ljava/lang/String ;) V #45 = Utf8 Son instance constructor!} Symbol reference#46 = Utf8 hello! This is son! #47 = Utf8 Son class constructor <clinit> RUN! #48 = Utf8 test/Son #49 = Utf8 test/Father #50 = Utf8 test/Father_interface #51 = Utf8 java/lang/System #52 = Utf8 out #53 = Utf8 Ljava /io/PrintStream; #54 = Utf8 append #55 = Utf8 (Ljava/lang/String;) Ljava/lang/StringBuilder; #56 = Utf8 (I) Ljava/lang/StringBuilder; #57 = Utf8 toString #58 = Utf8 () Ljava/lang/String; #59 = Utf8 java/io/PrintStream #60 = Utf8 println #61 = Utf8 (Ljava/lang/String;) V {public test. son ();Method table-instance ConstructorFlags: ACC_PUBLIC Code: stack = 3, locals = 1, args_size = 1 0: aload_0 1: invokespecial #1 // Method test/Father. "<init>" :() V 4: aload_0 5: bipush 123 7: putfield #2 // Field sId_literal: I 10: getstatic #3 // Field java/lang/System. out: Ljava/io/PrintStream; 13: new #4 // class java/lang/StringBuilder 16: dup 17: invokespecial #5 // Method java/lang/StringBuilder. "<init>" :() V 20: ldc #6 // String statica L_sId = 22: invokevirtual #7 // Method java/lang/StringBuilder. append :( Ljava/lang/String;) Ljava/lang/StringBuilder; 25: getstatic #8 // Field statical_sId: I 28: invokevirtual #9 // Method java/lang/StringBuilder. append :( I) Ljava/lang/StringBuilder; 31: invokevirtual #10 // Method java/lang/StringBuilder. toString :() Ljava/lang/String; 34: invokevirtual #11 // Method java/io/PrintStream. println :( Ljava /Lang/String;) V 37: getstatic #3 // Field java/lang/System. out: Ljava/io/PrintStream; 40: ldc #12 // String Son instance constructor! 42: invokevirtual #11 // Method java/io/PrintStream. println :( Ljava/lang/String;) V 45: sipush 333 48: putstatic #8 // Field statical_sId: I 51: return LineNumberTable: line 15: 0 line 7: 4 line 16: 10 line 17: 37 line 18: 45 line 19: 51 public void sayHello ();Method table -- sayHello ()Flags: ACC_PUBLIC Code: stack = 2, locals = 1, args_size = 1 0: getstatic #3 // Field java/lang/System. out: Ljava/io/PrintStream; 3: ldc #13 // String hello! This is son! 5: invokevirtual #11 // Method java/io/PrintStream. println :( Ljava/lang/String;) V 8: return LineNumberTable: line 21: 0 line 22: 8 static {};Method table-class Constructor (or static code block)Flags: ACC_STATIC Code: stack = 3, locals = 0, args_size = 0 0: bipush 123 2: putstatic #8 // Field statical_sId: I 5: getstatic #3 // Field java/lang/System. out: Ljava/io/PrintStream; 8: new #4 // class java/lang/StringBuilder 11: dup 12: invokespecial #5 // Method java/lang/StringBuilder. "<init>" :() V 15: ldc #6 // String statical_sId = 17: invokevirtual #7 // Method java/lang/StringBuilder. append :( Ljava/ Lang/String;) Ljava/lang/StringBuilder; 20: getstatic #8 // Field statical_sId: I 23: invokevirtual #9 // Method java/lang/StringBuilder. append :( I) Ljava/lang/StringBuilder; 26: invokevirtual #10 // Method java/lang/StringBuilder. toString :() Ljava/lang/String; 29: invokevirtual #11 // Method java/io/PrintStream. println :( Ljava/lang/String;) V 32: getstatic #3 // Field java/lang/System. out: Ljava/io/Print Stream; 35: ldc #14 // String Son class constructor <clinit> RUN! 37: invokevirtual #11 // Method java/io/PrintStream. println :( Ljava/lang/String;) V 40: return LineNumberTable: line 6: 0 line 11: 5 line 12: 32 line 13: 40}

 

 

 

3. Summary

Magic number, version number, constant pool, access flag, index, field table set, and method table set

Magic number Version Number Constant pool Access flag Index Field table set Method table set

The suffix can be changed,

So use it to determine the truth

Is the. class File

44 = JDK1.0

45 = jdk1.1

....

Literal: int, double, and other basic data values

Symbol reference :( finally point to the UTF-8 string)

1) Record

Access flag + fully qualified name

2) record Method

Simple name + return type + parameter list

3) record Member attributes

Simple name + return type

 

 

It can be divided into: this class, inherited parent class, and implemented Interface

Point to "constant pool-record class"

 

Each table:

Access flag, (Name and description [such as return type and parameters]), additional attribute attributes (Code Attribute Table, Linumber Attribute Table, local variable table, and exception table)

/** In parentheses, it indicates that it is pointing to the "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.