您的位置:首页 > 编程语言 > Java开发

Java程序编译和运行的过程相关

2014-10-22 13:35 429 查看
http://allenhk.iteye.com/blog/1866288

Java整个编译以及运行的过程相当繁琐,本文通过一个简单的程序来简单的说明整个流程。

如下图,Java程序从源文件创建到程序运行要经过两大步骤:1、源文件由编译器编译成字节码(ByteCode) 2、字节码由java虚拟机解释运行。因为java程序既要编译同时也要经过JVM的解释运行,所以说Java被称为半解释语言( "semi-interpreted" language)。



图1 java程序编译运行过程

关于JVM和JIT
http://iask.sina.com.cn/b/2072837.html
Java Virtual Machine 就是指Java虚拟器,以下简称VM.关于VM的概念,最早出自CPU模拟器,众所周知的PC上的游戏机模拟器采用的便是和Java VM类似的技术.java source code被编译成byte code后,其实已经是很类似机器代码了,只不过没有真正能运行这类code的CPU而已,于是为了能达到在不同平台上运行的目的,VM的概念被大力推广. 从简单的角度说,VM其实就是把java bytecode 翻译成特定的机器代码, 这和解释型编程语言不同,我们会在下面讲到.

第一代的VM大多采用直接解释的方法,也就是说一句一句地把bytecode翻译成机器代码. 但这里有个缺点,如遇到循环, VM就不得不一次次重复翻译已经被翻译过的代码. 而每一次翻译均需消耗一定的cpu时间. 于是出现了第二代的VM - JIT (Just In Time Compiler)它只在bytecode需要被翻译时才做翻译工作.怎么实现呢? 打个比方,JIT VM可以事先把一段bytecode翻成machine code,并把这段code放进buffer, 当运行循环是,就反复调用这段翻译好的代码! JIT的效率取决于他分析重复调用的本领,把翻译的工作量限制到最小,以便让出CPU时间来运行程序逻辑.

http://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do
JIT-Just in time the word itself says when it's needed(on demand)


Typical scenario:

The source code is completely converted into machine code


JIT scenario:

The source code will be converted into assembly language like structure [for ex IL(intermediate language) for C#,ByteCode for java].

The intermediate code is converted into machine language only when the application needs that is required codes are only converted to machine code.


JIT vs Non-JIT comparison:

In JIT not all the code is converted into machine code first a part of the code that is neccessary will be converted into machine code then if a method or functionality called is not in machine then that will be turned into machine code... it reduces burden
on cpu.

As the machine code will be generated on run time....the JIT compiler will produce machine code that is optimised for running machine's cpu architecture.


JIT Examples:

In Java JIT is JVM(java virtual machine)

In C# it is in dot net framework

In Android DVM(Dalvik virtual machine)

关于ByteCode
为什么需要bytecode
http://stackoverflow.com/questions/8153792/why-the-java-language-need-bytecode-why-java-design-in-this-way
Interpreting (and compiling) byte code is faster than interpreting raw java.

Byte code is portable. You can compile on Windows, and run the byte code on Mac or Unix.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: