Write Your Own Java Virtual Machine cover

Write Your Own Java Virtual Machine

No rating yet
Word count
144K words
Chapters
13
Series
Build Your Own Series
ISBN
9787111534136

Tags

Summary

The Java Virtual Machine is the cornerstone of the Java platform, but mainstream implementations such as OpenJDK are enormous and written in C++, so beginners easily lose their way in the code details. This book takes a different path: it neither interprets the specification documents nor analyzes the JDK source line by line. Instead, it starts from scratch in Go and builds, by hand, a Java virtual machine capable of running real class files. The book has 11 chapters, each layering new functionality onto the last: first a program skeleton built with the java command-line tool; then class-path searching and class-file parsing (magic number, version number, constant pool, attribute table); then the thread-private runtime data area (Java virtual machine stack, stack frames, local variable table, operand stack); then the instruction set and interpreter (including more than 150 bytecode instructions); and gradually support for classes and objects, method invocation and return, arrays and strings, class loading, and the method area. Chapter 9 discusses native method invocation and reflection, implementing basic methods such as Object.hashCode() and System.arraycopy(); Chapter 10 explains the exception-handling table and the athrow instruction; Chapter 11 traces the initialization of the System class and the execution of System.out.println(), bringing the book to a close. The code for each chapter can be compiled and run independently, so readers can work through it in order or skip around as needed.

Editor's review

很多人学JVM,最后只记住“栈帧”“方法区”几个名词,遇到真实问题仍然说不出所以然。这本书的做法很直接:与其读代码,不如自己写一个。作者选择Go语言而非C++,避开了C++的复杂语法,又能用结构体和接口把复杂度压到可控范围;书中每一章都在上一章的基础上叠加功能,从读取class文件到跑通System.out.println(),读者能清楚看到一台虚拟机是怎样被一块块拼起来的。更适合把它当作“动手手册”:每章末尾都有测试代码,照着敲一遍比读三遍收获更大。全书篇幅精简,267页、14.4万字,节奏紧凑,适合有Java基础、想补齐底层认知的程序员作为第一个“造轮子”项目。

Who it's for

A good fit for

  • 具备Java基础、想真正理解字节码与JVM运行机制的后端开发者
  • 希望用一个完整项目打通编译原理、类加载与解释器知识的程序员
  • 想通过“造轮子”提升工程能力的中高级工程师
  • 对Go语言有兴趣、希望以实战方式熟悉Go语法的读者

Not a good fit for

  • 完全没有编程基础、也从未接触过Java的入门读者
  • 只想速查JVM调优参数、不打算动手写代码的人
  • 期待深入讲解垃圾回收算法与GC调优的读者

Reading tips

  1. 按章节顺序边读边敲,每章跑通测试代码后再进入下一章,避免只读不写
  2. 第3章解析class文件时,配合javap -v查看真实字节码,对照书中的结构体字段理解
  3. 第5章指令集内容较多,可先实现常用指令跑通小程序,再回头补齐其余指令
  4. 遇到不熟悉的Go语法先跳过,重点放在数据结构设计与虚拟机执行流程上

About the publisher

China Machine Press China · Founded in 1950

In addition to science and technology and professional publishing, the books in our catalog from China Machine Press also reflect a sustained focus on psychology, computing, management, finance and economics, and social sciences. In psychology, the catalog includes works by C. G. Jung and introductions to analytical psychology, as well as applied topics such as stress and health, and addiction and healing. In computing, it features Structure and Interpretation of Computer Programs (JavaScript Edition). Management and finance titles include Administrative Behavior (4th Edition), The Price of Inequality (Collector's Edition), and This Time Is Different: Eight Centuries of Financial Folly (Commemorative Edition). In the philosophy and history of science, there is The Worldview: The Third Edition. Relevant authors include C. G. Jung, William McGuire, Sonu Shamdasani, and Gabor Maté. Public information is limited; the above summarizes the publisher's profile based solely on the books in our catalog.

Contents

  1. Preface
  2. Chapter 1: Command-Line Tool
  3. Chapter 2: Searching for Class Files
  4. Chapter 3: Parsing Class Files
  5. Chapter 4: Runtime Data Area
  6. Chapter 5: Instruction Set and Interpreter
  7. Chapter 6: Classes and Objects
  8. Chapter 7: Method Invocation and Return
  9. Chapter 8: Arrays and Strings
  10. Chapter 9: Native Method Invocation
  11. Chapter 10: Exception Handling
  12. Chapter 11: The End
  13. Appendix: Instruction Table

FAQ

这本书需要先学Go语言吗?
不需要系统学习。书中会简要介绍用到的Go语法及它与Java的差异,有C、C++、C#或Java等语言经验的读者基本能读懂源码;若要更顺畅,可先花半天了解Go的结构体、接口与包机制。
和《深入理解Java虚拟机》有什么区别?
《深入理解Java虚拟机》侧重讲解JVM规范、内存模型、垃圾回收与性能调优;本书则是一个动手项目,用Go从零实现一台能运行class文件的虚拟机,偏重“自己实现一遍”的体验,两者可以互补阅读。
这本书讲垃圾回收吗?
本书聚焦类路径搜索、class文件解析、运行时数据区、指令解释器、类加载与本地方法等实现环节,并未把垃圾回收算法与GC调优作为重点,想深入GC需另找专著。
跟着做能实现到什么程度?
按书中章节推进,最终可以运行包含数组、字符串、方法调用、异常处理以及System.out.println()等基础功能的Java程序,足以跑通经典小例子,但并非完整可用的生产级JVM。
书中的代码能直接运行吗?
每章代码都经过调整,除第1章外均建立在前一章基础上,同时每章又可单独编译运行;建议按版本对应下载或自行录入,并准备好JDK与Go环境,即可复现书中示例。

Comments

Find this book

This site is an index: you start “Search all sources” in your browser, and the entries it lists all point to third-party sites; please judge their availability yourself.

More by the same author

Top rated in the same category