JVM如何執(zhí)行的Java方法

舉例


public class Test{
????public static void main(String[] args){

? ? ????Calculator cal = new Calculator();

? ? ????cal.calculate();

????}

}

另一個(gè)類:

public Calculator{

? ? public void calculate(){

? ? ? ? int x = 0;

? ? ? ? int y = 1;

? ? ? ? Log.e("Calculate", "result is -->" + x/y);

}






流程:

1,首先,JVM開始運(yùn)行時(shí),會(huì)將Test.java、Calcultor.class 編譯成 Test.class文件

2,程序開始運(yùn)行時(shí),會(huì)運(yùn)行程序的入口方法? ?----? main方法

3,JVM首先為該程序創(chuàng)建 方法區(qū)、堆區(qū)、棧區(qū)等

4,運(yùn)行入口方法時(shí),JVM會(huì)將Test.class裝載進(jìn)內(nèi)存,然后在棧區(qū)中為main()方法創(chuàng)建一個(gè)棧幀

5,然后開始執(zhí)行?Calculator cal = new?Calculator();

6,首先會(huì)在方法區(qū)中裝載Calculator.class,然后需要在堆區(qū)去創(chuàng)建一個(gè)Calculator實(shí)例對(duì)象,

7,這個(gè)實(shí)例對(duì)象會(huì)保有著方法區(qū)中Calculator.class的引用,即知道自己是哪個(gè)class文件的

8,然后調(diào)用cal.calculate();時(shí),就會(huì)到Calculator類的方法表中進(jìn)行方法的查找,如果找到了calculate()方法,就會(huì)在棧中,為calculate()方法創(chuàng)建一個(gè)棧幀,

9,然后接下來就會(huì)運(yùn)行cal.calculate(); 此時(shí)就會(huì)去Calculator.class的字節(jié)碼文件中去查找calculate()的方法體,然后去執(zhí)行該方法體

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容