Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is javac source code is written in java? [closed]

I have downloaded the javac source code from here and I found that the it itself is written in java language. However, I was expecting that it was written in c/c++.

Anyway, how does this java compiler source code written in java compiled when there was no java compiler?

like image 829
bob zhou Avatar asked Oct 07 '12 07:10

bob zhou


3 Answers

From here :

The very first Java compiler developed by Sun Microsystems was written in C using some libraries from C++

Besides the compiled bytecode is interpreted by JVM which is written in c++. From here:

The Oracle JVM, named HotSpot, is written in the C++ language

like image 183
loxxy Avatar answered Oct 16 '22 18:10

loxxy


A compiler that is written in the language it compiles is called a bootstrapping compiler.

The way they are made is kind of a head trip, but just think: when the original language was written, there was no java, and so they had to create the compiler in another language, which, actually, was written in C/C++. Check it out, here: In which language are the Java compiler and JVM written?

Also, the way that Java works, I don't know if you know, is that the compiler (javac) actually doesn't generate machine code files, it creates bytecode files that are then interpreted by the JVM.

like image 44
alvonellos Avatar answered Oct 16 '22 17:10

alvonellos


You usually need an existing Java compiler (and runtime) to bootstrap. However, there are other Java compilers available, like Jikes, that are written in C++. Whether you can use Jikes to bootstrap OpenJDK is a different story, but in theory, it should be possible.

like image 44
Chris Jester-Young Avatar answered Oct 16 '22 17:10

Chris Jester-Young