Java 编译型语言和解释型语言有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2657268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:26:24  来源:igfitidea点击:

What's the difference between compiled and interpreted language?

javajavascriptprogramming-languages

提问by SIr Codealot

After reading some material on this subject I'm still not sure what the difference between a compiled language and an interpreted language is. I was told this is one of the differences between Java and JavaScript. Would someone please help me in understanding it?

在阅读了关于这个主题的一些材料后,我仍然不确定编译语言和解释语言之间的区别是什么。有人告诉我这是 Java 和 JavaScript 之间的区别之一。有人可以帮助我理解它吗?

采纳答案by Norman Ramsey

What's the difference between compiled and interpreted language?

编译型语言和解释型语言有什么区别?

The difference is notin the language; it is in the implementation.

区别在于语言;它正在实施中

Having got that out of my system, here's an answer:

把它从我的系统中取出来,这是一个答案:

  • In a compiled implementation, the original program is translated into native machine instructions, which are executed directly by the hardware.

  • In an interpreted implementation, the original program is translated into something else. Another program, called "the interpreter", then examines "something else" and performs whatever actions are called for. Depending on the language and its implementation, there are a variety of forms of "something else". From more popular to less popular, "something else" might be

    • Binary instructions for a virtual machine, often called bytecode, as is done in Lua, Python, Ruby, Smalltalk, and many other systems (the approach was popularized in the 1970s by the UCSD P-system and UCSD Pascal)

    • A tree-like representation of the original program, such as an abstract-syntax tree, as is done for many prototype or educational interpreters

    • A tokenized representation of the source program, similar to Tcl

    • The characters of the source program, as was done in MINT and TRAC

  • 在编译实现中,原始程序被翻译成本地机器指令,由硬件直接执行。

  • 在解释实现中,原始程序被翻译成别的东西。另一个称为“解释器”的程序然后检查“其他事物”并执行所需的任何操作。根据语言及其实现,有多种形式的“其他”。从更受欢迎到不那么受欢迎,“别的东西”可能是

    • 虚拟机的二进制指令,通常称为bytecode,就像在 Lua、Python、Ruby、Smalltalk 和许多其他系统中所做的那样(该方法在 1970 年代由 UCSD P-system 和 UCSD Pascal 普及)

    • 原始程序的树状表示,例如抽象语法树,就像许多原型或教育解释器所做的那样

    • 源程序的标记化表示,类似于 Tcl

    • 源程序的字符,如在 MINT 和 TRAC 中所做的那样

One thing that complicates the issue is that it is possible to translate (compile) bytecode into native machine instructions. Thus, a successful intepreted implementation might eventually acquire a compiler. If the compiler runs dynamically, behind the scenes, it is often called a just-in-time compiler or JIT compiler. JITs have been developed for Java, JavaScript, Lua, and I daresay many other languages. At that point you can have a hybrid implementation in which some code is interpreted and some code is compiled.

使问题复杂化的一件事是可以将字节码转换(编译)为本地机器指令。因此,一个成功的解释实现可能最终会获得一个编译器。如果编译器在幕后动态运行,则它通常被称为即时编译器或 JIT 编译器。JIT 已经为 Java、JavaScript、Lua 以及我敢说许多其他语言开发。那时你可以有一个混合实现,其中一些代码被解释,一些代码被编译。

回答by stakx - no longer contributing

Java and JavaScript are a fairly bad example to demonstrate this difference, because both are interpreted languages. Java (interpreted)and C (or C++) (compiled)might have been a better example.

Java 和 JavaScript 是证明这种差异的一个相当糟糕的例子,因为它们都是解释型语言。Java (解释型)和 C(或 C++)(编译型)可能是一个更好的例子。

Why the striked-through text?As this answercorrectly points out, interpreted/compiled is about a concrete implementation of a language, not about the language per se. While statements like "C is a compiled language" are generallytrue, there's nothing to stop someone from writing a C language interpreter. In fact, interpreters for C do exist.

为什么是删除线文本?正如这个答案正确指出的那样,解释/编译是关于语言的具体实现,而不是关于语言本身。虽然像“C 是一种编译语言”这样的陈述通常是正确的,但没有什么可以阻止某人编写 C 语言解释器。事实上,C 的解释器确实存在

Basically, compiled code can be executed directly by the computer's CPU. That is, the executable code is specified in the CPU's "native" language (assembly language).

基本上,编译后的代码可以直接由计算机的 CPU 执行。也就是说,可执行代码是用 CPU 的“原生”语言(汇编语言)指定的。

The code of interpreted languages however must be translated at run-time from any format to CPU machine instructions. This translation is done by an interpreter.

然而,解释语言的代码必须在运行时从任何格式转换为 CPU 机器指令。该翻译由口译员完成。

Another way of putting itis that interpreted languages arecode is translated to machine instructions step-by-step whilethe program is being executed, while compiled languages havecode has been translated beforeprogram execution.

把它的另一种方式是,解释语言代码翻译成机器指令一步一步的同时,正在执行的程序,而编译语言有代码已经被翻译之前执行程序。

回答by Salil

A compiler, in general, reads higher level language computer code and converts it to either p-code or native machine code. An interpreter runs directly from p-code or an interpreted code such as Basic or Lisp. Typically, compiled code runs much faster, is more compact, and has already found all of the syntax errors and many of the illegal reference errors. Interpreted code only finds such errors after the application attempts to interpret the affected code. Interpreted code is often good for simple applications that will only be used once or at most a couple times, or maybe even for prototyping. Compiled code is better for serious applications. A compiler first takes in the entire program, checks for errors, compiles it and then executes it. Whereas, an interpreter does this line by line, so it takes one line, checks it for errors, and then executes it.

通常,编译器读取高级语言计算机代码并将其转换为 p 代码或本机机器代码。解释器直接从 p 代码或解释过的代码(如 Basic 或 Lisp)运行。通常,编译后的代码运行得更快,更紧凑,并且已经发现了所有的语法错误和许多非法引用错误。只有在应用程序尝试解释受影响的代码后,解释代码才会发现此类错误。解释代码通常适用于只使用一次或最多使用几次的简单应用程序,甚至可能用于原型设计。编译后的代码更适合严肃的应用程序。编译器首先接收整个程序,检查错误,编译它然后执行它。而解释器逐行执行此操作,因此需要一行来检查错误,

If you need more information, just Google for "difference between compiler and interpreter".

如果您需要更多信息,只需谷歌搜索“编译器和解释器之间的差异”。

回答by Personman

It is a very murky distinction, and in fact generally not a property of a language itself, but rather of the program you are using to execute code in that language.

这是一个非常模糊的区别,实际上通常不是语言本身的属性,而是您用来执行该语言代码的程序的属性。

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.

然而,大多数语言主要以一种或另一种形式使用,是的,Java 本质上总是被编译,而 javascript 本质上总是被解释。

To compile source code is to run a program on it that generates a binary, executable file that, when run, has the behavior defined by the source. For instance, javac compiles human-readbale .java files into machine-readable .class files.

编译源代码就是在其上运行一个程序,该程序生成一个二进制可执行文件,该文件在运行时具有源代码定义的行为。例如,javac 将人类可读的 .java 文件编译成机器可读的 .class 文件。

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.

解释源代码是在其上运行一个程序,该程序立即产生定义的行为,而不生成中间文件。例如,当您的 Web 浏览器加载 stackoverflow.com 时,它会解释一堆 javascript(您可以通过查看页面源代码来查看)并产生这些页面具有的许多不错的效果 - 例如,upvoting 或小通知程序横在顶部的酒吧。

回答by Praveen Kishor

Interpreted language is executed at the run time according to the instructions like in shell scripting and compiled language is one which is compiled (changed into Assembly language, which CPU can understand ) and then executed like in c++.

解释型语言在运行时按照shell脚本中的指令执行,编译型语言是一种编译(变成CPU可以理解的汇编语言)然后像c++一样执行的语言。

回答by PGOEL

Here is the Basic Difference between Compiler vs Interpreter Language.

这是编译器与解释器语言之间的基本区别。

Compiler Language

编译器语言

  • Takes entire program as single input and converts it into object code which is stored in the file.
  • Intermediate Object code is generated
  • e.g: C,C++
  • Compiled programs run faster because compilation is done before execution.
  • Memory requirement is more due to the creation of object code.
  • Error are displayed after the entire program is compiled
  • Source code ---Compiler ---Machine Code ---Output
  • 将整个程序作为单个输入并将其转换为存储在文件中的目标代码。
  • 生成中间对象代码
  • 例如:C,C++
  • 编译后的程序运行速度更快,因为编译是在执行之前完成的。
  • 由于目标代码的创建,内存需求更多。
  • 整个程序编译后报错
  • 源代码---编译器---机器代码---输出

Interpreter Language:

口译语言:

  • Takes single instruction as single input and executes instructions.
  • Intermediate Object code is NOT generated
  • e.g: Perl, Python, Matlab
  • Interpreted programs run slower because compilation and execution take place simultaneously.
  • Memory requirement is less.
  • Error are displayed for every single instruction.
  • Source Code ---Interpreter ---Output
  • 将单个指令作为单个输入并执行指令。
  • 不生成中间目标代码
  • 例如:Perl、Python、Matlab
  • 解释程序运行速度较慢,因为编译和执行同时进行。
  • 内存需求较少。
  • 每条指令都会显示错误。
  • 源代码---解释器---输出