5

I am confused with jvm.
what does exactly jvm do takes bytycode and interpret to native code is native code assembly language?

4
  • When you interpret you just perform the operations as they appear. When you translate code into machine code it is no longer interpreted. Commented Nov 28, 2016 at 17:24
  • 1
    I 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 ? @PeterLawrey Commented Nov 28, 2016 at 18:01
  • 2
    It 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. Commented Nov 28, 2016 at 18:03
  • thanks @PeterLawrey Commented Nov 28, 2016 at 18:04

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Late to this party by 5 years. I'm wondering whether at this point it's more beneficial to "aggressively optimize" (whatever that means) the entire code base instead of having two steps?
In short, aggressive optimization could be to just ignore an addition instruction because a certain value seems to "always" be 0. That is "aggressive" in that it can at some point change and the JIT has to realize this and put back the addition instruction again.
8

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 is like Assembly?
@M.T No. Bytecode is kind of like assembly. And the JIT produces machine code (also like assembly). Neither is actually an assembler or assembly language.
jvm input = bytecode (platform independent) , jvm output = binary code for target architecture.
0
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.