Compiling is a process to conversion from one level of abstraction to lower level. Meanwhile transpiling is a process of conversion from one level of abstraction to another at same level like converting java code to Kotlin/python. That is my understanding of the two process. Could someone please explain it in terms of java code and jvm byte code. And is my inference correct?
-
Keeping your definitions in mind, converting Java source code to byte code would not be considered transpiling, since byte code is at a lower level of abstraction than Java source code.Jesper– Jesper2020-05-26 05:56:46 +00:00Commented May 26, 2020 at 5:56
-
yup that's my understanding of transpiling.. but yeah i came across this term recently and i do not have complete understanding of transpiling.Rahul– Rahul2020-05-26 05:58:42 +00:00Commented May 26, 2020 at 5:58
3 Answers
Higher/Lower levels of abstraction from human language to machine language
A compiler translates from a higher level language to a lower level language. By higher/lower we mean how abstracted from machine language. So this would include Java language to bytecode. Bytecode is closer to machine language, and further away from human language.
A transpiler converts between languages of comparable levels of abstraction. Converting from EcmaScript 6 to EcmaScript 5 for compatibility with older web browsers would be one example. Converting from Java language to Kotlin would be another, or Swift to Kotlin.
See Wikipedia: https://en.wikipedia.org/wiki/Source-to-source_compiler
Intermediate representation
In particular, bytecode compiled from Java language and bitcode compiled via LLVM (from Swift, Rust, etc) are known as Intermediate Representation (IR). An IR is designed for further processing, optimizing, and translating on its way to becoming machine language.
Comments
Would conversion from java code to jvm byte code considered compiling or transpiling?
It is compiling, according to the definitions that you give in your question. The bytecode instruction set is at a lower level of abstraction than Java source code.
Having said that, this distinction between compiling and transpiling is a bit nebulous, since there is no clear definition of a "level of abstraction".
For example, one could argue that since C is sometimes called "high level assembly language", C++ is at a higher level of abstraction than C. So does that make C++ -> C conversion compilation? Or are they at the same level of abstraction, which makes C++ -> C conversion transpilation? This is not totally academic because the first implementation of the C++ language did translate C++ to C source code ... IIRC.
2 Comments
It works as explained below:
Firstly java source code is converted into Bytecode file by the translator named “Compiler”. The byte code file gets name with .class extension and javac (java compiler) is the tool to compile the .java file.
Then,
javais a tool use to invoke Java Interpreter “JVM”. Now, the work of JVM starts. When JVM invoke,
a subprogram in JVM called Class loader (or system class loader) starts and load the bytecode into OS memory( or RAM).
another subprogram Bytecode Verifier verify and ensure that the code do not violate the security rules. That’s why the java program is much secured and virus free.
Then last subprogram Execution Engine finally converts bytecodes into machine code. The name of that engine are in use today is JIT Just In Compiler.
You can read about the same here : https://www.quora.com/How-does-the-Java-interpreter-JVM-convert-bytecode-into-machine-code