views:

435

answers:

5

Hey all, 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?

+10  A: 

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.

Jon Skeet
I was unaware such a spec even existed. Thats really helpful.
faceless1_14
+7  A: 

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.

Brian Agnew
+1  A: 

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.

Mike McQuaid
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.
faceless1_14
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.
Mike McQuaid
+1  A: 

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

Paul Brinkley
A: 

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/

KitsuneYMG