+ 8

Missunderstanding with C relation with some modern programming languages?

I've always thought that most modern programming languages get intepreted or compiled into C or C++, but now I feel I have misunderstood it somehow. I know that Python gets intepreted into C, but does compiled languages such as Java actually get compiled into C? Or does Java some otherway get turned into machine code for example by JVM? How about JavaScript, (C++), PHP and C#? BASIC might not be itepreted or compiled into C...

27th Sep 2019, 3:03 PM
Seb TheS
Seb TheS - avatar
11 Respuestas
+ 6
Seb TheS Well! So the java compiler (javac) compiles your java code to JVM bytecode. Your computer doesn't know what to do with JVM bytecode because it's not a JVM. Oracle's Java Runtime Environment (JRE) Is written in C++ I think, but of course it was compiled to a .exe before you installed it. The JRE then interprets the JVM bytecode when you run your java code, so you could kind of say a C++ program runs your Java. But during all of what you did there was never any C or C++ involved, no.
27th Sep 2019, 3:38 PM
Schindlabua
Schindlabua - avatar
+ 10
If we want to be correct, then "compiling" means machine code pops out at the end, usually a .exe file; turning language A into B is called "transpiling" but it's a really silly word. C/C++ are compiled. C# is compiled with the C# compiler (csc) Java code gets compiled to JVM bytecode. It's machine code, but for the "java virtual machine", not a phyiscal computer. And then the JRE pretends to be a JVM and interprets the bytecode. Javascript is interpreted in the browser, so, by a C++ runtime (chrome) or a Rust runtime (firefox). (cf. aot-compilation) PHP is interpreted. There is the Zend compiler but it costs $. If you create a new language you don't have a way to compile it yet, and to get around that chicken-egg problem you often transpile your first compiler to C (bootstrapping). A common thing to do today is to compile to LLVM bytecode, and then the LLVM compiler does all the optimizations so you don't have to, and does the final step of creating an .exe. I'm out of space so I had to be brief sorry
27th Sep 2019, 3:24 PM
Schindlabua
Schindlabua - avatar
+ 4
just to add some more info from Schindlabua 's post now you might think, why would java use the javac to compile itself into a java bytecode before being interpreted by the JVM. why don't we just skip into interpreting on JVM instantly to make it faster and simpler? well the reason java has to compile itself into a bytecode first is the reason java is multi-platform. the bytecode is a sort of multi-platform machine code that can be run on any platform which has the JVM. while the JVM is platform dependant and it's job would be to convert the bytecode into a machine code that is specific to that platform ex. windows.
28th Sep 2019, 2:38 PM
Shen Bapiro
Shen Bapiro - avatar
+ 4
Schindlabua so how was the very first C compiler compiled? Edit: I just Googled it. It didn't have to be compiled. So the first C compiler (by Dennis Ritchie) was apparently written in Assembly language on something like a PDP-11 machine.
29th Sep 2019, 12:19 AM
Sonic
Sonic - avatar
+ 3
Javascript not compiled lang., it is an interpreted language. the code is executed simply by the interpreter, it is not compiled anywhere, so we can always see it in the browser by looking at the source code of the page.
27th Sep 2019, 3:24 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
To elaborate on the bootstrapping part: The Haskell compiler for example is written in Haskell, and that's how it goes for most languages. But how do you compile the first Haskell compiler? You have to leverage an existing compiler, like gcc.
27th Sep 2019, 3:28 PM
Schindlabua
Schindlabua - avatar
+ 3
if compiler or interpreter is written in C it doesn't mean code of language is compiled to C. It is compiled usualy to native machine language or interpreted as it. Java is special, is compiled to java byte code, like kotlin, scala. JVM execute instructions of bytecode, it is native in result (as every executed codes), but it is not generate new native exe file. kotlin can be translate to JS too as its second feature. C++ runs native. JS is interpreted, but is partly optimised to native by browser. Some parts of java and python library are native.
27th Sep 2019, 3:34 PM
zemiak
+ 3
Sonic Prof. Brailsford from Computerphile once mentioned that they were not sure who wrote the first assembler, everyone at university just kind of had it. Must have been god or some equally capable programmer who wrote it in machine code!
29th Sep 2019, 12:50 AM
Schindlabua
Schindlabua - avatar
+ 2
this is really interesting question, and i really hope there will be a really good answer. but this is my understanding on this, most of modern language use C when they're writing their interpreter/compiler. so the code are running in a computer "as-if" they're (the interpreter) a C program by giving a instruction from our code to running intrtpreter. please note i'm no expert in this.
27th Sep 2019, 3:18 PM
Taste
Taste - avatar
+ 2
Schindlabua So if I ran a Java program I wouldn't in the end be running a C program?
27th Sep 2019, 3:32 PM
Seb TheS
Seb TheS - avatar
+ 1
Schindlabua, I like your approach, colleague, what came first? chicken or egg?
27th Sep 2019, 3:32 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar