简体   繁体   English

为什么需要编译Java代码,但JavaScript代码不需要编译

[英]Why does Java code need to be compiled but JavaScript code does not

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学生,所以请原谅这个问题的天真。

Historically, JavaScript was an interpreted language. 从历史上看,JavaScript是一种解释型语言。 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. 编译器将高级语言转换为较低级语言,本机处理器或VM(在本例中为Java VM)可以直接执行。 This is much faster. 这要快得多。

JavaScript in modern browsers is now compiled on the fly. 现在浏览器中的JavaScript现在可以即时编译。 So when the script is loaded, the first thing the JavaScript engine does is compile it into a bytecode and then execute it. 因此,当加载脚本时,JavaScript引擎所做的第一件事就是将其编译成字节码然后执行它。 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的要求。

Java was from the getgo a language that always had an explicit compile step. Java来自getgo是一种始终具有明确编译步骤的语言。 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. 像IntelliJ或Eclipse这样的IDE可以动态编译Java,并且在许多情况下可以删除显式编译步骤。

JavaScript and Java are not the same thing. JavaScript和Java不是一回事。 They might share a similar name, but I refer you to the JS guru - Douglas Crockford to help clear up the fact that they are really not related at all. 他们可能会分享一个相似的名字,但我会把你推荐给JS大师 - 道格拉斯·克罗克福德,以帮助他们澄清他们真的根本不相关的事实。

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; 在浏览器的上下文中,Java以与Flash或Silverlight相同的方式运行 - 需要一个插件,浏览器充当该插件的主机; which hosts a Java runtime environment. 它托管Java运行时环境。

Javascript was designed to be a scripting language for the browser, and that's why the browser can understand it natively. Javascript被设计为浏览器的脚本语言,这就是浏览器可以原生地理解它的原因。 How the 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. 或者它可以执行一些JIT以使代码更接近硬件并且(希望)提高速度。

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. 它在许多语言中更简单,更适合两者中的一种 - Java是静态类型的,并且关于程序含义的动态相对较少,所以你可以编译它,从而做一些本来需要做的工作。运行。 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. JavaScript是动态类型的,你不能在运行时之前决定很多东西(例如+是加法还是连接),因此编译不能提供很多性能。 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 do compile, and in fact V8 never interprets anything. 然后就是现代JavaScript实现编译,事实上V8 从不解释任何东西。

Because of the being complexity of compiling level between Java and Javascript, there are some limitations in Javascript. 由于Java和Javascript之间编译级别的复杂性,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. 由于字节码是在为特定操作系统编写的JVM平台上执行的,因此硬件字节码执行对访问系统资源具有更多优势。 Even C code can be embedded into Java bytecode aswell. 甚至C代码也可以嵌入到Java字节码中。 On the other hand Since Javascript is only run on browser there is less to do with it. 另一方面,由于Javascript仅在浏览器上运行,因此与它无关。

there are two main part at Java platform. Java平台有两个主要部分。 Java programming Language and JVM. Java编程语言和JVM。 It makes each part focus only their own area. 它使每个部分只关注自己的区域。 That is why JVM doesnot dedal with Java programming syntax. 这就是JVM不会使用Java编程语法的原因。 It is similar to that when running C code linking does not deal with C code but assembly. 它类似于运行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. 它证明我们需要编程水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM