了解 Java 字节码和 JVM
在最近提出的一个问题中,我简单的回答突出了我对 Java、JVM 以及代码如何编译和运行的许多误解。 这让我产生了一种将我的理解提升到更低水平的愿望。 我对汇编这样的低级理解没有任何问题,但字节码和 JVM 让我感到困惑。 我不知道面向对象的代码是如何在低层次上分解的。 我想知道是否有人对如何学习 JVM、字节码和 Java 的底层功能有任何建议。 是否有任何实用程序允许您直接编写和运行字节码,因为我相信实践经验是加深对它的理解的最佳方式? 此外,关于该主题的阅读建议将不胜感激。
编辑:次要问题。 所以我有一个有点子问题,答案给了我一个有趣的想法来了解 jvm,仅以可读语法编写像 Brainf**k 或 Ook 这样的非常简单的语言的合理性是什么(也许我什至可以开发它)最终支持 oo)编译成字节码是? 这会是一次很好的学习经历吗?
In a recent question asked recently my simple minded answer highlighted many of my misconceptions about Java, the JVM, and how the code gets compiled and run. This has created a desire in me to take my understanding to a lower level. I have no problems with the low level understanding like assembly how ever bytecode and the JVM confound me. How object oriented code gets broken down on a low level is lost to me. I was wondering if anyone had any suggestion on how to learn about the JVM, bytecode and the lower level functioning of Java. Are there any utilities out there that allow you to write and run bytecode directly as I believe hands on experience with something is the best way to grow in understanding of it? Additionally and reading suggestions on this topic would be appreciated.
Edit: Secondary question. So I have a kinda sub question, the answers gave me an interesting idea to learn about the jvm, what would the plausibility of writing a really simple language like brainf**k or Ook only in a readable syntax (maybe I could even develop it to support oo eventually) that compiles into bytecode be? Would that be a good learning experience?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
建议阅读:JVM 规范。
您可能还想使用 BCEL - 还有其他库用于操作字节码,但这可能是最著名的一个。
Suggested reading: the JVM spec.
You might also want to play with BCEL - there are other libraries around for manipulating bytecode, but that's probably the best known one.
Apache BCEL 将允许您从字节码分析和手工制作 .class 文件。
javap 将允许您反汇编现有的 .class文件。 它对于构建快速测试类以了解幕后真正发生的事情特别有用。
The Apache BCEL will allow you to analyse and hand craft .class files from bytecode.
javap will allow you to disassemble existing .class files. It's particularly useful for knocking up quick test classes to understand what is really going on underneath the covers.
我通过阅读 ASM 教程 以及对库本身的研究。
恕我直言,ASM 比 BECL 更好。
I learned by reading the ASM tutorial and mucking about with the library itself.
IMHO, ASM is better than BECL.
只有一个可靠的来源可以帮助您理解 JVM
Java® 虚拟机规范 Java SE 7 版本
http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
There is only one reliable source for JVM understanding
The Java® Virtual Machine Specification Java SE 7 Edition
http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
Programming for the Java Virtual Machine 是一本关于此主题的好书。 (披露:我与作者合作。)
Programming for the Java Virtual Machine is a good book for this topic. (Disclosure: I work with the author.)
要了解 Java/JVM 的架构:请阅读 Wikipedia、规范和源代码。
为了理解面向对象的代码是如何在低级别上完成的:尝试用 C 等较低级别语言来模拟继承/多态/封装等功能。
在 C 中,您可以通过例如函数指针的组合来实现上述功能和嵌套结构。
For understanding Java/the JVM's architecture: read Wikipedia, the specs and the source code.
For understanding how object-orientated code is done on a low level: try and emulate features like inheritance/polymorphism/encapsulation in a lower-level language like C.
In C you can achieve the above through, for example, a combination of function pointers and nested structures.