一般来说,编译器读取高级语言的计算机代码并将其转换为 p 代码或本机代码。解释器直接从 p 代码或解释代码(如 Basic 或 Lisp)运行。通常,编译后的代码运行速度更快,更紧凑,并且已经发现了所有的语法错误和许多非法引用错误。解释的代码只有在应用程序尝试解释受影响的代码后才会发现此类错误。解释代码通常适用于只使用一次或最多几次的简单应用程序,甚至可以用于原型开发。对于严肃的应用程序,编译后的代码更好。编译器首先接收整个程序,检查错误,编译它,然后执行它。然而,解释器逐行执行此操作,因此它接受一行,检查它是否有错误,然后执行它。
However, most languages are used primarily in one form or the other, and yes, Java is essentially always compiled, while javascript is essentially always interpreted.
To interpret source code is run a program on it that produces the defined behavior right away, without generating an intermediary file. For instance, when your web browser loads stackoverflow.com, it interprets a bunch of javascript (which you can look at by viewing the page source) and produces lots of the nice effects these pages have - for instance, upvoting, or the little notifier bars across the top.
Notice that from a programmer point of view, CPUs are machine interpreters for their respective native machine language.
现在,我们可以根据最常见的实现将编程语言暂时分为三类:
硬编译语言: 当程序完全编译成机器语言时。唯一使用的解释器是 CPU。示例: 通常,要在 C 中运行程序,源代码被编译成机器语言,然后由 CPU 执行。
Interpreted languages: When there is no compilation of any part of the original program to machine language. In other words, no new machine code is generated; only existing machine code is executed. An interpreter other than the CPU must also be used (usually a program).Example: In the canonical implementation of Python, the source code is compiled first to Python 字节码 and then that bytecode is executed by CPython, an interpreter program for Python 字节码.
软编译语言: 使用 CPU 以外的解释器时,可以将原程序的某些部分编译成机器语言。这就是 Java 的情况,源代码首先被编译成字节码,然后字节码可以由 Java 解释器解释,或者由 JIT 编译器进一步编译。
Sometimes, soft and hard compiled languages are refered to simply compiled, thus C#, Java, C, C++ are said to be compiled.