18

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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Victor
  • 16,609
  • 71
  • 229
  • 409
  • 4
    Javascript is interpreted by the browser, not by the actual hardware of the computer (like C assembly) for one. – thatidiotguy Aug 07 '12 at 20:18
  • 2
    @thatidiotguy wat. JavaScript is rarely interpreted these days (V8 of Chrome fame doesn't even *have* an interpreter), "C assembly" does not make any sense at all, and assembly code is not executed at all, it is merely simple to turn into machine code. Although I have to give you props for realizing machine code is just interpreted at the end of the day too. –  Aug 07 '12 at 20:27
  • 1
    possible duplicate of [Compiled vs. Interpreted Languages](http://stackoverflow.com/questions/3265357/compiled-vs-interpreted-languages) – jbabey Aug 07 '12 at 20:30

4 Answers4

25

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 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 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.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • yep!Now a days i can see only run button in ide's for java – Ganesh Chowdhary Sadanala Nov 27 '18 at 15:18
  • Also want to point out that modern web browsers are moving back to interpreters. They are doing a hybrid approach where they start out interpreting, then switch to compiling. This allows the JavaScript to get up and running faster. – Matt Greer Nov 28 '18 at 16:17
3

JavaScript and Java are not the same thing. 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.

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).

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.

Javascript was designed to be a scripting language for the browser, and that's why the browser can understand it natively. 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.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
2

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.

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 do compile, and in fact V8 never interprets anything.

0

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.

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.

Bytecode in JVM platform is likely an assembly in C.

Eventually all representations are converted into binary representation and then the electirical signals somehow. It proves that we need programming levels.

Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • C code cannot be embedded in JVM code. Well, it probably can but it won't be executed. The CLR does allow `unsafe` code, but that's an entirely different thing. Also, regarding JS and the outside world, see V8, node.js and common.js. –  Aug 07 '12 at 20:29
  • I mean using JNI. You can call any method in object files (dll, so) – Ahmet Karakaya Aug 07 '12 at 20:31
  • Okay, that's true (I wouldn't call that "embedding in java bytecode" though). But you can also expose native code via any JavaScript engine. –  Aug 07 '12 at 20:32
  • Actually as you may know that 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. – Ahmet Karakaya Aug 07 '12 at 20:37
  • I know that, but I don't know how this is relevant. –  Aug 07 '12 at 20:38