So gcc or any C compiler will compile your source to x86 or your respective Assembly language then assemble the assembly into binary and then link it to create an executable. My question is does the java compiler contain an assembler? Meaning is there an intermediate stop because I understand bytecode isn't machine code but it is stored as binary and isn't plain text either. I'm assuming it's just the text based bytecode stored in binary format but I really don't know.
-
3That compilers must compile to assembly is a common misconception. They *can* work that way. Or they can skip that step and turn their internal representation directly into machine code. Or these days, it's also common to dump the internal representation in the object files, and then have the linker do nearly everything - in particular this enables whole-program optimization. – harold Feb 26 '15 at 12:10
2 Answers
Like you said, the Java Compiler generates bytecode. This bytecode then goes into the Java Virtual Machine which magically "makes the code happen". Since the JVM is a specification which has multiple implementation this magical part can be Just In Time compilation or simply interpreting. Related question here.
So to answer your question, no, the java compiler does not contain an assembler. The JVM may contain one but isn't forced to because that's implementation detail.
The Java compiler generates byte code in a binary format which is then translated into machine code at runtime by the Java virtual machine (JVM). It is important that this step happens at runtime because that allows Java programs to run on multiple platforms without recompilation.
You can observe the bytecode in a class file using the javap
utility which is part of the Java development kit (JDK). You can assemble your own bytecode using a utility like Jasmin, the Java assembler.

- 88,405
- 25
- 200
- 352
-
One kind of side question. If Java doesn't assemble the code it just goes from the compiler to the class then is the -c option on javap basically just mean display the bytecode because it wouldn't really be disassembling it? – Scoopta Feb 26 '15 at 18:20
-
@Scoopta It's disassembling the bytecode as it's translating the bytecode from its binary representation into a textual representation. – fuz Feb 26 '15 at 18:22
-
I kinda get it. Basically what I was confused by is it says disassemble but if the code is just stored in binary format then what is there to disassemble it would just be converting it into text and adding some of the comments it puts in. – Scoopta Feb 26 '15 at 18:25