java 解释器 工作原理,Java解释器或任何解释器如何工作?

I have been figuring out the exact working of an interpreter, have googled around and have come up with some conclusion, just wanted it to be rectified by someone who can give me a better understanding of the working of interpreter.

So what i have understood is:

An interpreter is a software program that converts code from high level

language to machine format.

speaking specifically about java interpreter, it gets code in binary format

(which is earlier translated by java compiler from source code to bytecode).

now platform for a java interpreter is the JVM, in which it runs, so

basically it is going to produce code which can be run by JVM.

so it takes the bytecode produces intermediate code and the target machine

code and gives it to JVM.

JVM in turns executes that code on the OS platform in which JVM is

implemented or being run.

Now i am still not clear with the sub process that happens in between i.e.

interpreter produces intermediate code.

interpreted code is then optimized.

then target code is generated

and finally executed.

Some more questions:

so is the interpreter alone responsible for generating target code ? and

executing it ?

and does executing means it gets executed in JVM or in the underlying OS ?

解决方案

An interpreter is a software program that converts code from high level language to machine format.

No. That's a compiler. An interpreter is a computer program that executes the instructions written in a language directly. This is different from a compiler that converts a higher level language into a lower language. The C compiler goes from C to assembly code with the assembler (another type of compiler) translates from assembly to machine code -- modern C compilers do both steps to go from C to machine code.

In Java, the java compiler does code verification and converts from Java source to byte-code class files. It also does a number of small processing tasks such as pre-calculation of constants (if possible), caching of strings, etc..

now platform for a java interpreter is the JVM, in which it runs, so basically it is going to produce code which can be run by JVM.

The JVM operates on the bytecode directly. The java interpreter is integrated so closely with the JVM that they shouldn't really be thought of as separate entities. What also is happening is a crap-ton of optimization where bytecode is basically optimized on the fly. This makes calling it just an interpreter inadequate. See below.

so it takes the bytecode produces intermediate code and the target machine code and gives it to JVM.

The JVM is doing these translations.

JVM in turns executes that code on the OS platform in which JVM is implemented or being run.

I'd rather say that the JVM uses the bytecode, optimized user code, the java libraries which include java and native code, in conjunction with OS calls to execute java applications.

now i am still not clear with the sub process that happens in between i.e. 1. interpreter produces intermediate code. 2. interpreted code is then optimized. 3. then target code is generated 4. and finally executed.

The Java compiler generates bytecode. When the JVM executes the code, steps 2-4 happen at runtime inside of the JVM. It is very different than C (for example) which has these separate steps being run by different utilities. Don't think about this as "subprocesses", think about it as modules inside of the JVM.

so is the interpreter alone responsible for generating target code ? and executing it ?

Sort of. The JVM's interpreter by definition reads the bytecode and executes it directly. However, in modern JVMs, the interpreter works in tandem with the Just-In-Time compiler (JIT) to generate native code on the fly so that the JVM can have your code execute more efficiently.

In addition, there are post-processing "compilation" stages which analyze the generated code at runtime so that native code can be optimized by inlining often-used code blocks and through other mechanisms. This is the reason why the JVM load spikes so high on startup. Not only is it loading in the jars and class files, but it is in effect doing a cc -O3 on the fly.

and does executing means it gets executed in JVM or in the underlying OS ?

Although we talk about the JVM executing the code, this is not technically correct. As soon as the byte-code is translated into native code, the execution of the JVM and your java application is done by the CPU and the rest of the hardware architecture.

The Operating System is the substrate that that does all of the process and resource management so the programs can efficiently share the hardware and execute efficiently. The OS also provides the APIs for applications to easily access the disk, network, memory, and other hardware and resources.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值