SlideShare ist ein Scribd-Unternehmen logo
1 von 36
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

Weitere ähnliche Inhalte

Was ist angesagt?

9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
Fundamentals of oop lecture 2
Fundamentals of oop lecture 2Fundamentals of oop lecture 2
Fundamentals of oop lecture 2
miiro30
 

Was ist angesagt? (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?
 

Ähnlich wie Let's talk about java class file

บทที่ 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
bank_b
 
Java input output package
Java input output packageJava input output package
Java input output package
Sujit Kumar
 
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
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and characters
alvin567
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

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 虚拟机》