I am confused with jvm.
what does exactly jvm do takes bytycode and interpret to native code is native code assembly language?
-
When you interpret you just perform the operations as they appear. When you translate code into machine code it is no longer interpreted.Peter Lawrey– Peter Lawrey2016-11-28 17:24:17 +00:00Commented Nov 28, 2016 at 17:24
-
1I am not clear with it. JVM is interpreter or it is used to be interpreter? since modern JVMs read bytecode from .class files, run it through the JIT to compile it to machine code. JVM interpreter or ? @PeterLawreyM.T– M.T2016-11-28 18:01:42 +00:00Commented Nov 28, 2016 at 18:01
-
2It starts by interpreting it and once the code is run enough e.g. 10K times it is compiled in the background to native code. In fact with teired compliation it goes through a number of stages of compilation and can be deoptimised as required.Peter Lawrey– Peter Lawrey2016-11-28 18:03:04 +00:00Commented Nov 28, 2016 at 18:03
-
thanks @PeterLawreyM.T– M.T2016-11-28 18:04:38 +00:00Commented Nov 28, 2016 at 18:04
3 Answers
is native code assembly language?
Assembly language is a way of writing code that will be assembled (by an assembler) into machine code, which is what is written to executable files and such. That is, assembly code is source code for humans, just at a very low level; machine code is the result of running an assembler on that source code. (This is analogous to when you write a higher-level language like C++ and compile it to machine code with a compiler.)
what does exactly jvm do takes bytycode and interpret to native code
A JVM could be written that just interpreted bytecode, but modern JVMs don't do that; they have a built-in Just-In-Time Compiler (JIT) which takes the bytecode and, effectively, assembles it on the-fly into machine code. In fact, Sun's JVM has a two-stage JIT: One stage that runs really quickly (so apps and classes get converted to machine code quickly when run, to avoid startup delays), and another stage it kicks in that does aggressive optimization, which it uses when it identifies "hot spots" (code that runs a lot) in the code (so that performance-critical code runs quickly).
So modern JVMs read bytecode from .class files, run it through the JIT to compile it to machine code, and have the computer run that machine code. While doing that, a good one monitors hot spots and aggressively optimizes them, creating new, replacement machine code that's more efficient.
2 Comments
Native code (or machine code) is what assembly language is compiled into. Macros are expanded, and then mnemonic OP codes are translated into binary machine code. The JIT doesn't use macros, it generates machine code directly (without an assembler).
3 Comments
- JVM isn't platform independent, instead JVM provides platform independent feature to Java code(byte code).
- When byte code is generated after compiling your written code, it can be taken to any operating system for running it which is possible due to JVM (specific to OS) which converts the byte code into the machine language depending on the OS.
- So, yes, bytecode(.class file) is converted to machine language (native code) by JVM by interpreting the bytecode.