为什么需要编译Java代码却不需要JavaScript代码 [英] Why does Java code need to be compiled but JavaScript code does not

查看:59
本文介绍了为什么需要编译Java代码却不需要JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么用Java编写的代码需要以JVM解释的字节码进行编译,但是用JavaScript之类的语言编写的代码却不需要进行编译并且可以直接在浏览器中运行?

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?

有一种简单的方法可以理解这一点吗?

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.

推荐答案

从历史上看,JavaScript是一种解释型语言.这意味着解释器接受源代码并一步一步执行所有源代码.这里的优点是简单和灵活,但是解释器很慢.编译器将高级语言转换为本机处理器或VM(在这种情况下为Java VM)可以直接执行的低级语言.这要快得多.

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现在可以即时编译.因此,在加载脚本时,JavaScript引擎要做的第一件事就是将其编译为字节码,然后执行它.从最终用户的角度来看,缺少整个编译步骤的原因是,浏览器开发人员一直(感激地)坚持不显式编译JavaScript的要求.

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.

Java是从getgo开始的一种语言,总是有一个明确的编译步骤.但是在许多情况下,这不再是真的.像IntelliJ或Eclipse这样的IDE可以即时编译Java,并且在许多情况下,可以省去显式的编译步骤.

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代码却不需要JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆