r/learnjava • • Aug 02 '20

Does java have multiple compilers like C++?

So C++ has various compilers that can be used to uphold the standard such as clang or mingw. When I tried to research for Java, I could not come across different compilers, I may have researched wrong. Any reason for the difference between the two languages? I have only seen javac talked about on the web.

50 Upvotes

10 comments sorted by

20

u/8igg7e5 Aug 02 '20

It's worth noting that the source compiler plays a much smaller role in Java. The compilation from source to byte-code typically, and arguably ideally, performs very little in the way of optimisations and the differences in compilers are more about the ergonomics (performance, error feedback).

At runtime the Java Virtual Machine can also (and nearly always does) employ Just In Time compilation and the choice and stages of compilation here play a much bigger role. There are several JVMs with differing JIT compilers but Java Hotspot is the most common. OpenJ9 is another.

Hotspot employs two stages of compilation, C1 and C2, where C1 is focused on fast compilation (because even poorly optimised code is faster that interpreted code) and C2 is focused on performance. Both compilers are guided, to varying degrees, by runtime statistics to guide optimisation choices and to choose which code should even be compiled. Code can be initially compiled in C1 and as statistics change be recompiled in C2 (and even then can have statistics on slow paths to hint when even C2 should consider recompilation).

5

u/Merry_Macabre Aug 02 '20

List of Compilers for various programming languages. Scroll down to java to see the list of available compilers for java.

4

u/abcoolynr Aug 02 '20

yes. openJdk, adoptJdk, oraclejdk etc.

0

u/[deleted] Aug 02 '20

[deleted]

4

u/BorgerBill Aug 02 '20

They are all named the same thing, but are different software written by different people...

1

u/abcoolynr Aug 02 '20

javac is a command not a compiler. each of openJDK, AdoptJDK, OracleJDK have written compilers separately underneath the jdk. All of them allow same Java syntax doesn't mean that compilers are same. All of them have different optimizations, security for the code.

8

u/[deleted] Aug 02 '20

They are all builds of OpenJDK. Where did you get the idea they rewrote the compiler?

0

u/[deleted] Aug 02 '20

[deleted]

5

u/abcoolynr Aug 02 '20

no, they don't need to get certified. OpenJDK is open source, you can find the code on github. It is the most widely used JDK today.

1

u/[deleted] Aug 02 '20

There's an IBM java compiler which compiles source directly to native code and not bytecode iirc

1

u/BenRayfield Aug 11 '20 edited Aug 11 '20

Javassist compiles in 0.1 second at runtime but doesn't support generics or lambda syntax. Can still compile a subclass of lambda types. Most importantly, javassist is a Java compiler made entirely of java. Or at least in the old fork I use for eval sometimes.