学习 Java 字节码和 JVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1207124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Learning about Java bytecode and the JVM
提问by faceless1_14
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.
在最近提出的一个问题中,我头脑简单的回答强调了我对 Java、JVM 以及代码如何编译和运行的许多误解。这让我产生了一种将我的理解降低到较低水平的愿望。我对像汇编这样的低级理解没有任何问题,字节码和 JVM 使我感到困惑。我不知道面向对象的代码是如何在低级别分解的。我想知道是否有人对如何学习 JVM、字节码和 Java 的低级功能有任何建议。是否有任何实用程序可以让您直接编写和运行字节码,因为我相信亲身体验某些东西是理解它的最佳方式?此外,阅读有关此主题的建议将不胜感激。
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?
编辑:次要问题。所以我有一个子问题,答案给了我一个有趣的想法来了解 jvm,仅以可读的语法编写像 Brainf**k 或 Ook 这样非常简单的语言的合理性如何(也许我什至可以开发它)最终支持oo)编译成字节码是什么?这会是一次很好的学习经历吗?
采纳答案by Jon Skeet
Suggested reading: the JVM spec.
建议阅读:JVM 规范。
You might also want to play with BCEL- there are other libraries around for manipulating bytecode, but that's probably the best known one.
您可能还想使用BCEL- 周围还有其他用于操作字节码的库,但这可能是最著名的库。
回答by Brian Agnew
The Apache BCELwill allow you to analyse and hand craft .class files from bytecode.
在Apache的BCEL将允许你从字节码分析和手工工艺的.class文件。
javapwill 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.
javap将允许您反汇编现有的 .class 文件。这对于启动快速测试类以了解幕后真正发生的事情特别有用。
回答by KitsuneYMG
I learned by reading the ASM tutorialand mucking about with the library itself.
我通过阅读ASM 教程和对库本身的了解来学习。
IMHO, ASM is better than BECL.
恕我直言,ASM 比 BECL 好。
BCEL is already being used successfully in several projects such as compilers, optimizers, obsfuscators, code generators and analysis tools. Unfortunately there hasn't been much development going on over the past few years. Feel free to help out or you might want to have a look into the ASM project at objectweb.- http://jakarta.apache.org/bcel/
BCEL 已经在多个项目中成功使用,例如编译器、优化器、混淆器、代码生成器和分析工具。不幸的是,过去几年并没有太大的发展。随时提供帮助,或者您可能想在 objectweb 上查看 ASM 项目。- http://jakarta.apache.org/bcel/
回答by Ivan Pierre
There is only one reliable source for JVM understanding
JVM理解只有一种可靠的来源
The Java? Virtual Machine Specification Java SE 7 Edition
爪哇?虚拟机规范 Java SE 7 版
http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
回答by Paul Brinkley
Programming for the Java Virtual Machineis a good book for this topic. (Disclosure: I work with the author.)
Java 虚拟机编程是本主题的好书。(披露:我与作者合作。)
回答by Mike McQuaid
For understanding Java/the JVM's architecture: read Wikipedia, the specs and the source code.
要了解 Java/JVM 的体系结构:请阅读 Wikipedia、规范和源代码。
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.
为了理解面向对象的代码是如何在低层次上完成的:尝试用像 C 这样的低层次语言来模拟继承/多态/封装等特性。
In C you can achieve the above through, for example, a combination of function pointers and nested structures.
在 C 中,您可以通过例如函数指针和嵌套结构的组合来实现上述目的。

