Search code examples
javac++compiler-construction

Difference between C++ and Java compilation process


I searched in google for the differences between C++ and Java compilation process, but C++ and Java language features and their differences are returned.

I am proficient in Java, but not in C++. But I fixed few bugs in C++. From my experience, I noticed that C++ always took more time to build compared to Java for minor changes.


Solution

  • There are a few high-level differences that come to my mind. Some of those are generalizations and should be prefixed with "Often ..." or "Some compilers ...", but for the sake of readability I'll leave that out.

    • C/C++ compilation doesn't read any information from binary files, but reads method/type definitions only from header files that need to be parsed in full (exception: precompiled headers)
    • C/C++ compilation includes a pre-processor step that can do a wide array of text-replacement (which makes header pre-compilation harder to do)
    • The C++ syntax is a lot more complex than the Java syntax
    • The C++ type system is a lot more complex than the Java type system
    • C++ compilation usually produces native assembler code, which is a lot more complex to produce than the relatively simple byte code
    • C++ compilers need to do optimizations because there isn't any other thing that will do them. The Java compiler pretty much does a simple 1:1 translation of Java source code to Java byte code, no optimizations are done at that step (that's left for the JVM to do).
    • C++ has a template language that's Turing complete! (so strictly speaking C++ code needs to be run to produce executable code and a C++ compiler would need to solve the halting problem to tell you if arbitrary C++ code is compilable).