从 java 代码到 jvm 字节码的转换是否考虑编译或转译?

would conversion from java code to jvm byte code considered compiling or transpiling?

编译是从一个抽象层次向低层次抽象转换的过程。同时,转译是从一个抽象级别转换为同一级别的另一个抽象级别的过程,例如将 java 代码转换为 Kotlin/python。这就是我对这两个过程的理解。有人可以用 java 代码和 jvm 字节代码来解释它吗?我的推断是否正确?

它的工作原理如下:

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,

java is a tool use to invoke Java Interpreter “JVM”. Now, the work of JVM starts. When JVM invoke,

  1. a subprogram in JVM called Class loader (or system class loader) starts and load the bytecode into OS memory( or RAM).

  2. 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.

  3. 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.

您可以在此处阅读相同内容:https://www.quora.com/How-does-the-Java-interpreter-JVM-convert-bytecode-into-machine-code

Higher/Lower 从人类语言到机器语言的抽象层次

编译器将高级语言翻译成低级语言。 higher/lower 是指从机器语言中抽象出来的程度。所以这将包括 Java 语言到字节码。字节码更接近机器语言,更远离人类语言

转译器在抽象级别相当的语言之间进行转换。从 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 转换为 Kotlin。

参见维基百科:https://en.wikipedia.org/wiki/Source-to-source_compiler

中间表示

特别是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.

Would conversion from java code to jvm byte code considered compiling or transpiling?

正在根据您在问题中给出的定义进行编译。字节码指令集的抽象级别低于 Java 源代码。


话虽如此,编译和转译之间的区别有点模糊,因为“抽象级别”没有明确的定义。

例如,有人可能会争辩说,由于 C 有时被称为“高级汇编语言”,因此 C++ 比 C 处于更高的抽象级别。那么 C++ -> C 转换编译吗?或者它们处于同一抽象级别,这使得 C++ -> C 转换转译?这并不完全是学术性的,因为 C++ 语言的第一个实现 did 将 C++ 翻译成 C 源代码 ... IIRC。