40

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?

2
  • 2
    +1 for giving brainf**k the attribute 'simple' ;-) Commented Jul 30, 2009 at 15:40
  • 1
    I guess "simple" was used in a sense of small complexity, not ease of use ;)
    – SasQ
    Commented Jun 9, 2015 at 21:31

6 Answers 6

31

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.

1
  • I was unaware such a spec even existed. Thats really helpful. Commented Jul 30, 2009 at 15:08
13

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.

5

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/

3
  • And Krakatau is much better than ASM if you actually want to write bytecode by hand.
    – Antimony
    Commented Aug 4, 2013 at 16:26
  • sorry,link the ASM tutorial has gone dead
    – zionpi
    Commented Aug 28, 2013 at 12:33
  • 1
    @zionpi, there's always Archive.org ( web.archive.org/web/20070715000000/http://… )
    – SasQ
    Commented Jun 9, 2015 at 21:35
3

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

2

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

1

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.

4
  • I really like this second suggestion. I think I may have to attempt to come up with an elegant way of doing that. I know of some really convoluted ones for polymorphism and inheritance but they are all really inefficient techniques I came up with that probably don't even really count as emulating the features. Commented Jul 30, 2009 at 15:00
  • Added more information about doing this in C. It's actually not that hard when you have a good understand of the language and how pointers work. Commented Jul 30, 2009 at 16:06
  • @MikeMcQuaid: What about implementing closures in C?
    – SasQ
    Commented Jun 9, 2015 at 21:38
  • To approximate a closure in C, split it into the struct which holds the captures and the function whose first argument is that struct. This is how plain C libraries do callbacks, see pthread_create for an example
    – Riley
    Commented Jan 21, 2022 at 23:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.