Learning about Java bytecode and the JVM

2019-03-07 15:01发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

I learned by reading the ASM tutorial and mucking about with the library itself.

IMHO, ASM is better than 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/



回答4:

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



回答5:

Programming for the Java Virtual Machine is a good book for this topic. (Disclosure: I work with the author.)



回答6:

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.