javascript 为什么Java代码需要编译而JavaScript代码不需要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11853423/
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
Why does Java code need to be compiled but JavaScript code does not
提问by Victor
How come code written in Java needs to be compiled in byte-code that is interpreted by the JVM, but code written in a language like JavaScript does not need to be compiled and can run directly in a browser?
为什么用Java编写的代码需要编译成JVM解释的字节码,而用JavaScript这样的语言编写的代码不需要编译,可以直接在浏览器中运行?
Is there an easy way to understand this?
有没有一种简单的方法来理解这一点?
What is the fundamental difference between the way these two languages are written, that may help to understand this behavior?
这两种语言的编写方式之间的根本区别是什么,可能有助于理解这种行为?
I am not a CS student, so please excuse the naivete of the question.
我不是CS学生,所以请原谅这个问题的幼稚。
回答by Matt Greer
Historically, JavaScript was an interpreted language. Which means an interpreter accepts the source code and executes it all in one step. The advantage here is simplicity and flexibility, but interpreters are very slow. Compilers convert the high level language into a lower level language that either the native processor or a VM (in this case, the Java VM) can execute directly. This is much faster.
从历史上看,JavaScript 是一种解释型语言。这意味着解释器接受源代码并一步执行。这里的优点是简单和灵活,但解释器很慢。编译器将高级语言转换为本地处理器或 VM(在本例中为 Java VM)可以直接执行的低级语言。这要快得多。
JavaScript in modern browsers is now compiled on the fly. So when the script is loaded, the first thing the JavaScript engine does is compile it into a bytecode and then execute it. The reason the entire compilation step is missing from the end user's perspective is because browser developers have (thankfully) maintained the requirement that JavaScript is not explicitly compiled.
现代浏览器中的 JavaScript 现在可以即时编译。所以当脚本被加载时,JavaScript 引擎做的第一件事就是将它编译成字节码,然后执行它。从最终用户的角度来看,缺少整个编译步骤的原因是浏览器开发人员(幸好)维持了不显式编译 JavaScript 的要求。
Java was from the getgo a language that always had an explicit compile step. But in many cases that's not true anymore. IDE's like IntelliJ or Eclipse can compile Java on the fly and in many cases remove the explicit compilation step.
Java 从一开始就是一种始终具有显式编译步骤的语言。但在许多情况下,这不再是真的。IDE 像 IntelliJ 或 Eclipse 可以即时编译 Java,并且在许多情况下删除显式编译步骤。
回答by Andras Zoltan
JavaScript and Java are not the same thing. They might share a similar name, but I refer you to the JS guru - Douglas Crockfordto help clear up the fact that they are really not related at all.
JavaScript 和 Java 不是一回事。它们可能具有相似的名称,但我向您推荐JS 大师 - Douglas Crockford以帮助澄清它们实际上根本不相关的事实。
The reality is that there is nothing stopping Java being an interpreted language, and there's equally nothing stopping JavaScript being a compiled language (Chrome's javascript engine does do compilation to improve speed, and does a very good job of it).
现实情况是,没有什么能阻止 Java 成为一种解释型语言,同样也没有什么能阻止 JavaScript 作为一种编译型语言(Chrome 的 javascript 引擎确实通过编译来提高速度,并且做得非常好)。
In the context of the browser, Java runs in the same way as Flash or Silverlight - a plugin is required and the browser acts as a host to that plugin; which hosts a Java runtime environment.
在浏览器的上下文中,Java 的运行方式与 Flash 或 Silverlight 相同——需要一个插件,浏览器充当该插件的宿主;它承载 Java 运行时环境。
Javascript was designed to be a scripting language for the browser, and that's why the browser can understand it natively. Howthe browser actually achieves the running of that code, however, is entirely up to the browser. That is - it can operate purely at a script level, assuming zero-knowledge of the next line of code and running a purely software-based stack; or it can perform some JIT to get the code closer to the hardware and (hopefully) improve speed.
Javascript 被设计为浏览器的脚本语言,这就是浏览器可以本地理解它的原因。 然而,浏览器如何实际实现该代码的运行完全取决于浏览器。也就是说 - 它可以纯粹在脚本级别运行,假设下一行代码的知识为零并运行纯粹基于软件的堆栈;或者它可以执行一些 JIT 以使代码更接近硬件并(希望)提高速度。
回答by Andras Zoltan
Any language can be compiled and interpreted. In both cases, a piece of software has to read the source code, split it up, parse it, etc. to check certain requirements and then assign a meaning to every part of the program. The only difference is that the compiler then proceeds to generate code with (almost) the same meaning in another language (JVM bytecode, or JavaScript, or machine code, or something entirely else) while the interpreter carries out the meaning of the program immediately.
任何语言都可以编译和解释。在这两种情况下,软件都必须读取源代码、对其进行拆分、解析等,以检查某些要求,然后为程序的每个部分分配一个含义。唯一的区别是编译器然后继续生成具有(几乎)相同含义的另一种语言(JVM 字节码,或 JavaScript,或机器代码,或完全其他的东西)的代码,而解释器立即执行程序的含义。
Now, in practice it's both simpler and more complicated. It's simpler in many languages lend themselves better to one of the two - Java is statically-typed and there is relatively little dynamic about the meaning of a program, so you can compile it and thus do some work which would otherwise need to be done at runtime. JavaScript is dynamically-typed and you can't decide a lot of things (such as whether + is addition or concatenation) until runtime, so compilation does not afford you much performance. However, a mix of compiler and interpreter (compile to simplified intermediate representation, then interpret and/or compile that) is increasingly popular among dynamic language implementations. And then there's the fact that modern JavaScript implementations docompile, and in fact V8 neverinterprets anything.
现在,在实践中它既简单又复杂。它在许多语言中更简单,更适合两者之一 - Java 是静态类型的,并且程序的含义相对较少,因此您可以编译它,从而完成一些原本需要在运行。JavaScript 是动态类型的,在运行之前您无法决定很多事情(例如 + 是加法还是串联),因此编译不会为您提供太多性能。然而,编译器和解释器的混合(编译为简化的中间表示,然后解释和/或编译它)在动态语言实现中越来越流行。还有一个事实是现代 JavaScript 实现会编译,事实上 V8从来没有解释任何事情。
回答by Ahmet Karakaya
Because of the being complexity of compiling level between Java and Javascript, there are some limitations in Javascript. Since bytecode is executed on JVM platform which is written for specific OS and Hardware bytecode execution has more advantages to access system resources. Even C code can be embedded into Java bytecode aswell. On the other hand Since Javascript is only run on browser there is less to do with it.
由于Java和Javascript之间编译级别的复杂性,Javascript存在一定的局限性。由于字节码是在为特定操作系统编写的 JVM 平台上执行的,因此硬件字节码执行在访问系统资源方面具有更多优势。甚至 C 代码也可以嵌入到 Java 字节码中。另一方面,由于 Javascript 仅在浏览器上运行,因此与它无关。
there are two main part at Java platform. Java programming Language and JVM. It makes each part focus only their own area. That is why JVM doesnot dedal with Java programming syntax. It is similar to that when running C code linking does not deal with C code but assembly.
Java平台有两个主要部分。Java 编程语言和 JVM。它使每个部分只关注自己的领域。这就是 JVM 不处理 Java 编程语法的原因。类似于运行 C 代码时,链接不处理 C 代码,而是处理汇编。
Bytecode in JVM platform is likely an assembly in C.
JVM 平台中的字节码很可能是 C 中的程序集。
Eventually all representations are converted into binary representation and then the electirical signals somehow. It proves that we need programming levels.
最终所有的表示都被转换成二进制表示,然后以某种方式转换为电信号。这证明我们需要编程水平。