SlideShare a Scribd company logo
Let’s Talk about
Java Class File
Yongqiang Li
Outlines
 Brief
      Introduction to Java Class File
 Java Class Format and Layout
 References
Brief Introduction to Java Class
 File
 Has  .class extension
 Compiled from java source files (.java)
 Platform-independent
 Machine-readable
 Stores so-called “bytecodes”
Java Source File (ClassA.java)

                                      javac ClassA.java




     Java Class File (ClassA.class)
 Bytecode
 Only   “javac” can generate class file?

          JRuby                   Clojure




Jython             Java Class


          Groovy                   Scala
Java Class Format and Layout
   Class File contains different sections.
       Magic Number
       Version of Class File Format
       Constant Pool
       Access Flags
       This Class
       Super Class
       Interfaces
       Fields
       Methods
       Attributes
Magic Number
4 bytes
 Always “CAFEBABE” ( 咖啡宝贝 )
Version of Class File Format

  2  bytes for minor version
   2 bytes for major version
   Java major version starts from 45 (JDK 1.1)
   Java SE 5 is 49, SE 6 is 50 (0x32), SE 7 is 51.
Constant Pool
2  bytes used to store constant count
 Index of constant starts from 1 not 0.
Tag Byte   Constant Type           Additional Bytes
0x01       UTF8-String             2+x
0x03       Integer                 4 (Big endian)
0x04       Float                   4 (IEEE 754 floating-
                                   point)
0x05       Long                    8 (Big endian)

0x06       Double                  8 (IEEE 754 floating-
                                   point)
0x07       Class Reference         2
0x08       String Reference        2
0x09       Field Reference         2+2 (first 2 for class
                                   Ref)
0x0A       Method Reference        2+2 (first 2 for class
                                   Ref)
0x0B       Interface Method Ref.   2+2 (first 2 for class
                                   Ref)
0x0C       Name and Type Ref.      2+2
 Constant    Count




 0x0021(hex) = 33 (dec)
 Double




           • 0x06: Double Tag
           • 0x4000 1893 74bc 6a7f: 2.012
• 0x01: UTF-8 Tag
 UTF-8   • 0x0006: “String” has 6 char.
          • 0x5374 7269 6367: “String”
 More   Strings
    Field/Method
     Name
    Field/Method
     Type
    Attribute Name
 Class   Reference




                      • 0x07: Class Ref Tag
                      • 0x0002: Constant Index
Access Flags

 For Class
 2 Bytes
This and Super
  Both  are 2 bytes
  Direct to Class Reference in Constant
   Pool.




This Ref: 0x0001
Super Ref: 0x0003
Interfaces
2  bytes for count of interfaces
 2 * interfaces_count bytes
Fields
2  bytes for fields count
 field_info * fields_count
 Attribute   Structure
 Field   Access
     2 bytes
•   0x0001: Only 1 Field
•   0x0019: public static final
•   0x0005: “obj”
•   0x0006: Descriptor “Ljava/lang/Object;”
•   0x0002: 2 Attributes
     • 1st: 0x0007 (Deprecated)
     • 2nd: 0x0008 (RuntimeVisibleAnnotations)
Methods
2 bytes for methods count
 method_info * methods_count

                          Method Access Flags
 Method Attributes
 CodeAttributes
•   Max Stack: 2
•   Max Locals: 3
•   Args Size: 2
•   Code:
 JVM   Opcode
    Based on stack
     not register
    About 200
     opcode in JVM 6
 0: push the integer in
  slot 0 to stack
 1: push the integer in
  slot 1 to stack
 2: add first 2 integers in
  stack and result will be
  push into stack
 3: load the integer
  from stack top to
  integer in slot 2
 4: push the integer in
  slot 2 to stack
 5: return an integer
 Exception   Attributes
Who can help me on this?
 Magic Number
 Version of Class File Format
 Constant Pool
 Access Flags
 This Class
 Super Class
 Interfaces
 Fields
 Methods
 Attributes
References
 JVM
    6 Specification
 《深入浅出 Java 虚拟机》
Let's talk about java class file

More Related Content

What's hot

Java Advanced Features
Java Advanced FeaturesJava Advanced Features
Java Advanced Features
Michael Redlich
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
mhtspvtltd
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
Chap01
Chap01Chap01
Chap01
Jotham Gadot
 
Fundamentals of oop lecture 2
Fundamentals of oop lecture 2Fundamentals of oop lecture 2
Fundamentals of oop lecture 2
miiro30
 
Ifi7184 lesson7
Ifi7184 lesson7Ifi7184 lesson7
Ifi7184 lesson7
Sónia
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
manish kumar
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
kanchanmahajan23
 

What's hot (10)

Java Advanced Features
Java Advanced FeaturesJava Advanced Features
Java Advanced Features
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Chap01
Chap01Chap01
Chap01
 
Fundamentals of oop lecture 2
Fundamentals of oop lecture 2Fundamentals of oop lecture 2
Fundamentals of oop lecture 2
 
Ifi7184 lesson7
Ifi7184 lesson7Ifi7184 lesson7
Ifi7184 lesson7
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 

Similar to Let's talk about java class file

บทที่4
บทที่4บทที่4
บทที่4
Waritsara Sonchan
 
Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
Marc Tritschler
 
Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
marctritschler
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
People Strategists
 
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูลบทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
Priew Chakrit
 
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
squall1735
 
บท4
บท4บท4
บท4
kiddy67
 
บท4
บท4บท4
บท4
bank_b
 
Introduction_to_Java.ppt
Introduction_to_Java.pptIntroduction_to_Java.ppt
Introduction_to_Java.ppt
HemantSharma134028
 
Java input output package
Java input output packageJava input output package
Java input output package
Sujit Kumar
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
Christopher Akinlade
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
Woxa Technologies
 
Java quickref
Java quickrefJava quickref
Java quickref
Arduino Aficionado
 
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Stronger
snyff
 
Java14
Java14Java14
Java14
aiter2002
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
Woxa Technologies
 
ORC File & Vectorization - Improving Hive Data Storage and Query Performance
ORC File & Vectorization - Improving Hive Data Storage and Query PerformanceORC File & Vectorization - Improving Hive Data Storage and Query Performance
ORC File & Vectorization - Improving Hive Data Storage and Query Performance
DataWorks Summit
 
ORC File and Vectorization - Hadoop Summit 2013
ORC File and Vectorization - Hadoop Summit 2013ORC File and Vectorization - Hadoop Summit 2013
ORC File and Vectorization - Hadoop Summit 2013
Owen O'Malley
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and characters
alvin567
 

Similar to Let's talk about java class file (20)

บทที่4
บทที่4บทที่4
บทที่4
 
Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
 
Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูลบทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
 
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
4.เพิ่มข้อมูลลง DB ด้วย JSP&SQL
 
บท4
บท4บท4
บท4
 
บท4
บท4บท4
บท4
 
Introduction_to_Java.ppt
Introduction_to_Java.pptIntroduction_to_Java.ppt
Introduction_to_Java.ppt
 
Java input output package
Java input output packageJava input output package
Java input output package
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Java quickref
Java quickrefJava quickref
Java quickref
 
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Stronger
 
Java14
Java14Java14
Java14
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
ORC File & Vectorization - Improving Hive Data Storage and Query Performance
ORC File & Vectorization - Improving Hive Data Storage and Query PerformanceORC File & Vectorization - Improving Hive Data Storage and Query Performance
ORC File & Vectorization - Improving Hive Data Storage and Query Performance
 
ORC File and Vectorization - Hadoop Summit 2013
ORC File and Vectorization - Hadoop Summit 2013ORC File and Vectorization - Hadoop Summit 2013
ORC File and Vectorization - Hadoop Summit 2013
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and characters
 

More from Yongqiang Li

Why Kotlin?
Why Kotlin?Why Kotlin?
Why Kotlin?
Yongqiang Li
 
How to Recognize Henry's Face
How to Recognize Henry's FaceHow to Recognize Henry's Face
How to Recognize Henry's Face
Yongqiang Li
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regex
Yongqiang Li
 
Let's talk about java class loader
Let's talk about java class loaderLet's talk about java class loader
Let's talk about java class loader
Yongqiang Li
 
Brief introduction to domain-driven design
Brief introduction to domain-driven designBrief introduction to domain-driven design
Brief introduction to domain-driven design
Yongqiang Li
 
Eclipse GEF (Part I)
Eclipse GEF (Part I)Eclipse GEF (Part I)
Eclipse GEF (Part I)
Yongqiang Li
 
Garbage Collection of Java VM
Garbage Collection of Java VMGarbage Collection of Java VM
Garbage Collection of Java VM
Yongqiang Li
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jni
Yongqiang Li
 

More from Yongqiang Li (8)

Why Kotlin?
Why Kotlin?Why Kotlin?
Why Kotlin?
 
How to Recognize Henry's Face
How to Recognize Henry's FaceHow to Recognize Henry's Face
How to Recognize Henry's Face
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regex
 
Let's talk about java class loader
Let's talk about java class loaderLet's talk about java class loader
Let's talk about java class loader
 
Brief introduction to domain-driven design
Brief introduction to domain-driven designBrief introduction to domain-driven design
Brief introduction to domain-driven design
 
Eclipse GEF (Part I)
Eclipse GEF (Part I)Eclipse GEF (Part I)
Eclipse GEF (Part I)
 
Garbage Collection of Java VM
Garbage Collection of Java VMGarbage Collection of Java VM
Garbage Collection of Java VM
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jni
 

Recently uploaded

Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 

Recently uploaded (20)

Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Artificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic WarfareArtificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic Warfare
 

Let's talk about java class file

  • 1. Let’s Talk about Java Class File Yongqiang Li
  • 2. Outlines  Brief Introduction to Java Class File  Java Class Format and Layout  References
  • 3. Brief Introduction to Java Class File  Has .class extension  Compiled from java source files (.java)  Platform-independent  Machine-readable  Stores so-called “bytecodes”
  • 4. Java Source File (ClassA.java) javac ClassA.java Java Class File (ClassA.class)
  • 6.  Only “javac” can generate class file? JRuby Clojure Jython Java Class Groovy Scala
  • 7. Java Class Format and Layout  Class File contains different sections.  Magic Number  Version of Class File Format  Constant Pool  Access Flags  This Class  Super Class  Interfaces  Fields  Methods  Attributes
  • 8.
  • 9. Magic Number 4 bytes  Always “CAFEBABE” ( 咖啡宝贝 )
  • 10. Version of Class File Format 2 bytes for minor version  2 bytes for major version  Java major version starts from 45 (JDK 1.1)  Java SE 5 is 49, SE 6 is 50 (0x32), SE 7 is 51.
  • 11. Constant Pool 2 bytes used to store constant count  Index of constant starts from 1 not 0.
  • 12. Tag Byte Constant Type Additional Bytes 0x01 UTF8-String 2+x 0x03 Integer 4 (Big endian) 0x04 Float 4 (IEEE 754 floating- point) 0x05 Long 8 (Big endian) 0x06 Double 8 (IEEE 754 floating- point) 0x07 Class Reference 2 0x08 String Reference 2 0x09 Field Reference 2+2 (first 2 for class Ref) 0x0A Method Reference 2+2 (first 2 for class Ref) 0x0B Interface Method Ref. 2+2 (first 2 for class Ref) 0x0C Name and Type Ref. 2+2
  • 13.  Constant Count 0x0021(hex) = 33 (dec)
  • 14.  Double • 0x06: Double Tag • 0x4000 1893 74bc 6a7f: 2.012
  • 15. • 0x01: UTF-8 Tag  UTF-8 • 0x0006: “String” has 6 char. • 0x5374 7269 6367: “String”
  • 16.  More Strings  Field/Method Name  Field/Method Type  Attribute Name
  • 17.  Class Reference • 0x07: Class Ref Tag • 0x0002: Constant Index
  • 18. Access Flags  For Class  2 Bytes
  • 19. This and Super  Both are 2 bytes  Direct to Class Reference in Constant Pool. This Ref: 0x0001 Super Ref: 0x0003
  • 20. Interfaces 2 bytes for count of interfaces  2 * interfaces_count bytes
  • 21. Fields 2 bytes for fields count  field_info * fields_count
  • 22.  Attribute Structure
  • 23.  Field Access  2 bytes
  • 24. 0x0001: Only 1 Field • 0x0019: public static final • 0x0005: “obj” • 0x0006: Descriptor “Ljava/lang/Object;” • 0x0002: 2 Attributes • 1st: 0x0007 (Deprecated) • 2nd: 0x0008 (RuntimeVisibleAnnotations)
  • 25. Methods 2 bytes for methods count  method_info * methods_count Method Access Flags
  • 26.  Method Attributes  CodeAttributes
  • 27. Max Stack: 2 • Max Locals: 3 • Args Size: 2 • Code:
  • 28.  JVM Opcode  Based on stack not register  About 200 opcode in JVM 6
  • 29.
  • 30.  0: push the integer in slot 0 to stack  1: push the integer in slot 1 to stack  2: add first 2 integers in stack and result will be push into stack  3: load the integer from stack top to integer in slot 2  4: push the integer in slot 2 to stack  5: return an integer
  • 31.
  • 32.  Exception Attributes
  • 33. Who can help me on this?  Magic Number  Version of Class File Format  Constant Pool  Access Flags  This Class  Super Class  Interfaces  Fields  Methods  Attributes
  • 34.
  • 35. References  JVM 6 Specification  《深入浅出 Java 虚拟机》