Forums Register Login
Understading how java compilation and execution really works ?
I read a couple of books and articles which give elementary explanations about how it works - java converted to bytecode, VM understands bytecode and runs it, you see output.
This is too basic and far from sufficient. I want to understand the process in detail. I don't know how compilers and such work, but I am willing to learn. I tried the link below, which
has a little more, but its not enough depth...


Can you suggest books or websites that explain the minute details from creating a java file to executing it ?

This is not anything near a beginners topic, but if you really want the details, The Java Language Specification has all the details as you could ever want (and then some). Generally speaking it is beyond what you need to know in order to program Java, but is a good source to look up specifics when you need them. Chapters 1-11 is details about the Java lexicon and syntax - it basically covers the 'compiler' end of things (though I think there is some run-time considerations in there). From chapter 12 on, the consideration is mostly run-time.
Also (again well beyond what is required for programming) there is the Java 7 JVM Specification which (among other things) describes the byte code format, how loading, linking, and initialization occurs.

So, to summarize and add a little bit:
There is no real one way as far as the details of compilation and run times. Instead, there are two specification. The JLS defines the syntax and lexicon which basically describes how a compiler should translate the Java code into byte code - and provides some definition as to how it would work at run time. The JVMS defines the byte code format and specifies the behavior of translating that byte code into executable code. But there are different compilers and different run times and there is a lot of 'implementation details' which provides room for differences in specifics - which is okay as long as they meet the specifications.
Thanks Steve. Is there any text book which explains the stuff in the Java spec ? The spec can be a little daunting and even dry for a "beginner" like me.
Textbooks generally say the same stuff in a much easier to understand way.
And welcome to the Ranch

You know you can inspect the bytecode? Create a little class FooThat will show you the bytecode’s meaning. The printout looks very like assembler code. You can also inspect the bytecode by opening the .class file with a hex editor. There is an appendix to the JVM specification which tells you what the hex pairs mean. You can also read about the .class file format in the BCEL handbook, but that might be a little out of date.

Andy Jack wrote: . . . Is there any text book which explains the stuff in the Java spec ? . . .

If you mean about your original question, I don’t know of one. As SL implies, it is not something you actually need to know.
(1 like)
It's not Java specific, but if you're really interested in the theory about how compilers work (and I'd repeat the point that you don't need to know this stuff at all to write code, but it can be interesting), you could have a look at this: https://www.coursera.org/course/compilers

It's a free course on how compilers work. I'm just coming to the end of the scheduled version, but there's a self-study version where you can view the materials when you want.

Very briefly, most compilers contain five stages:
- Lexical analysis: breaking up the source code into individual tokens
- Parsing: working out the structure of the code
- Semantic analysis: checking things like type safety, not using variables that don't exist, etc
- Optimisation: trying to speed things up (or use less memory, or whatever) without affecting the behaviour
- Code generation: generating assembly language/Java byte code/whatever the target is.
If you move away from Java specifically, then understanding how compilers, or at least some of the stages, work actually becomes a lot more useful in the context of domain specific languages, which are commonly used for all sorts of things. This is a great book on language implementation patterns by the guy that created ANTLR. I woudn't recommend it if you're just starting out, though.

Just as an aside, Andy, I recognize the need to know the details of how stuff works. It's a pitfall that can be hugely counter-productive, though. Learning to accept that knowledge at some level of abstraction is sufficient to do what you need to do is difficult, but very, very necessary.
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 714 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 20, 2019 18:16:19.