Introduction to Java Virtual Machine
Total Page:16
File Type:pdf, Size:1020Kb
,QWURGXFWLRQWR-DYD9LUWXDO 0DFKLQH 2XWOLQH -DYD/DQJXDJH-DYD9LUWXDO0DFKLQH DQG-DYD3ODWIRUP 2UJDQL]DWLRQRI-DYD9LUWXDO0DFKLQH *DUEDJH&ROOHFWLRQ ,QWHUSUHWHUDQG-XVW,Q7LPH &RPSLOHU 7KH%LJ3LFWXUH $MDYD %MDYD &MDYD -DYD/DQJXDJH 6SHFLILFDWLRQ -DYD &RPSLOHU $FODVV %FODVV &FODVV -DYD9LUWXDO 0DFKLQH -DYD9LUWXDO0DFKLQH 6SHFLILFDWLRQ :KDW,VLQWKH-906SHF" %\WHFRGHV ² WKHLQVWUXFWLRQVHWIRU -DYD9LUWXDO0DFKLQH &ODVV)LOH)RUPDW ² 7KHSODWIRUP LQGHSHQGHQWUHSUHVHQWDWLRQRI-DYD ELQDU\FRGH 9HULILFDWLRQ5XOHV ² WKHDOJRULWKPIRU LGHQWLI\LQJSURJUDPVWKDWFDQQRW FRPSURPLVHWKHLQWHJULW\RIWKH-90 %\WHFRGHH[DPSOH 6WDFN 6WDFN 6WDFN 6WDFN 63→ 63→ 63→ 63→ 3&→ELSXVK ELSXVK ELSXVK ELSXVK ELSXVK 3&→ELSXVK ELSXVK ELSXVK LDGG LDGG 3&→LDGG LDGG 3&→ &ODVV)LOH([DPSOH +HOOR:RUOGMDYD public class HelloWorld extends Object { private String s; public HelloWorld() { s = ‘Hello World!’; } public void sayHello() { System.out.println(s); } public static void main(String[] args) { HelloWorld hello = new HelloWorld(); hello.sayHello( ); } } &ODVV)LOH([DPSOH +HOOR:RUOGFODVV class HelloWorld Superclass java/lang/Object Constant Pool #0: ‘Hello World’ Fields s descriptor : Ljava/lang/String; modifiers : private Methods <init> descriptor : ()V modifiers : public sayHello descriptor : ()V modifiers : public main descriptor : (Ljava/lan/String[;)V modifiers : public, static Bytecodes Bytecodes for <init> (the constrtuctor) Bytecodes for sayHello Byt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DWLRQHQVXULQJWKHFRUUHFWQHVVRIWKH LPSRUWHGW\SH 3UHSDUDWLRQDOORFDWLQJPHPRU\IRUFODVVYDULDEOHV DQGLQLWLDOL]LQJWKHPHPRU\WRGHIDXOWYDOXHV 5HVROXWLRQWUDQVIRUPLQJV\PEROLFUHIHUHQFHVIURP WKHW\SHLQWRGLUHFWUHIHUHQFHV ,QLWLDOL]DWLRQLQYRNLQJ-DYDFRGHWKDW LQLWLDOL]HVFODVVYDULDEOHVWRWKHLUSURSHU VWDUWLQJYDOXHV &ODVV$UHD &ODVV,QIRUPDWLRQ ² ,QWHUQDOUHSUHVHQWDWLRQRI-DYDFODVVHV ² ,QIRUPDWLRQDERXWWKH VXSHUFODVV DQG LPSOHPHQWHGLQWHUIDFHV ² ,QIRUPDWLRQDERXWWKHILHOGVDQGPHWKRGV &RQVWDQW3RRO 0HWKRG$UHD ² &RQWDLQVWKHE\WHFRGHV RIWKHPHWKRGVRI WKHORDGHGFODVVHV 6WDFN 7KH-DYDVWDFNLVFRPSRVHGRIIUDPHV ² $IUDPHFRQWDLQVWKHVWDWHRIRQH-DYDPHWKRG LQYRFDWLRQ ² /RJLFDOO\DIUDPHKDVWZRSDUWV ORFDOYDULDEOH DUUD\ DQGRSHUDQGVWDFN -90 KDVQRUHJLVWHUVLWXVHVWKHRSHUDQG VWDFN IRUVWRUDJHRILQWHUPHGLDWHGDWDYDOXHV ² WRNHHSWKH -90 V LQVWUXFWLRQVHWFRPSDFW ² WRIDFLOLWDWHLPSOHPHQWDWLRQRQDUFKLWHFWXUHVZLWK OLPLWHGQXPEHURIJHQHUDOSXUSRVHUHJLVWHUV (DFK-DYDWKUHDGKDVLWVRZQVWDFNDQGFDQQRW DFFHVVWKHVWDFNRIRWKHUWKUHDGV 6WDFN)UDPH public class A ,QWHU { 0HGLDWH 2SHUDQG ... ... 63→ 'DWD 6WDFN void f(int x) 9DOXHV { int i; L /RFDO for(i=0; i<x; i++) 9DULDEOH { [ $UUD\ ... ... &DOOHU·V63 } ... ... &DOOHU·V)3 } )3→ 5HWXUQ3& 6WDFN² (DFK7KUHDG+DVLWVRZQ6WDFN +HDS 7KUHDG 7KUHDG 7KUHDG )UDPH )UDPH )UDPH )UDPH )UDPH )UDPH )UDPH )UDPH 6WDFN +HDS $OO-DYDREMHFWVDUHDOORFDWHGLQWKH KHDS -DYDDSSOLFDWLRQVFDQQRWH[SOLFLWO\IUHH DQREMHFW 7KH*DUEDJH&ROOHFWRU LVLQYRNHGIURP WLPHWRWLPHDXWRPDWLFDOO\WRUHFODLP WKHREMHFWVWKDWDUHQRORQJHUQHHGHG E\WKHDSSOLFDWLRQ 7KHKHDSLVVKDUHGE\DOO-DYDWKUHDGV -DYD2EMHFWVLQWKH+HDS FOD]] )LHOGV )LHOGV … … FODVV2EMHFW )LHOGVQ FODVV$ FODVV% FOD]] … FOD]] &ODVV$UHD … +HDS *DUEDJH&ROOHFWRU 5RRWVLQWHUQDOO\GHILQHGE\WKH -90 LPSOHPHQWDWLRQ /LYH2EMHFWVUHDFKDEOHIURPWKHURRWV *DUEDJH 'HDG2EMHFWV QRWUHDFKDEOHIURPWKH URRWVQRWDFFHVVLEOHWRWKHDSSOLFDWLRQ URRW $ B ( ) & ' * 0DUN6ZHHS&RPSDFWLRQ URRW URRW URRW URRW $ $ % % % % ( & & * ' ' ( ( ( ) ) * * * %HIRUH *& $IWHU 0DUN $IWHU 6ZHHS $IWHU &RPSDFW /LYH *DUEDJH 8QNQRZQ )UHH *HQHUDWLRQDO*DUEDJH&ROOHFWLRQ 0RVWREMHFWVOLYHIRUYHU\VKRWWLPH $VPDOOSHUFHQWDJHRIWKHPOLYHPXFKORQJHU )UHH )UHH )UHH <RXQJ *HQHUDWLRQ <RXQJ *HQHUDWLRQ 2OG *HQHUDWLRQ <RXQJ *HQHUDWLRQ 2OG 2OG *HQHUDWLRQ *HQHUDWLRQ 2OG *HQHUDWLRQ ,QWHUSUHWHUYV -XVW,Q7LPH&RPSLOHU %\WHFRGH %\WHFRGH 1DWLYH -,7 ,QWHUSUHWHU &RGH &RPSLOHU &38 &38 ,QWHUSUHWDWLRQ -,7&RPSLODWLRQ %\WHFRGH,QWHUSUHWHU while(program not end ) { fetch next bytecode => b switch(b) { case ILOAD: load an integer from the local variable array and push on top of current operand stack; case ISTORE: pop an integer from the top of current operand stack and store it into the local variable array; case ALOAD: ….. ... } // end of switch } // end of while %\WHFRGHLQWHUSUHWHU $GYDQWDJH ² (DVHWRLPSOHPHQW ² 'RHVQRWQHHGH[WUDPHPRU\WRVWRUH FRPSLOHGFRGH 'LVDGYDQWDJH ² 9HU\6ORZaWLPHVVORZHUWKDQ H[HFXWLRQRIQDWLYHFRGH -XVW,Q7LPH&RPSLOHU '\QDPLFDOO\FRPSLOHVE\WHFRGHLQWR QDWLYHFRGHDWUXQWLPHXVXDOO\LQ PHWKRGJUDQXODULW\ ([HFXWLRQRIQDWLYHFRGHLVPXFK IDVWHUWKDQLQWHUSUHWDWLRQRIE\WHFRGH &RPSLODWLRQLVWLPHFRQVXPLQJDQGPD\ VORZGRZQWKHDSSOLFDWLRQ 7UDGHRIIVEHWZHHQH[HFXWLRQWLPHDQG FRPSLODWLRQWLPH $GDSWLYH&RPSLOHU 2EVHUYDWLRQOHVVWKDQRIWKHPHWKRGV DFFRXQWIRUPRUHWKDQRIH[HFXWLRQWLPH ² 0HWKRGVFRQWDLQVORRSZLWKODUJHQXPEHURI LWHUDWLRQ ² 0HWKRGVWKDWDUHIUHTXHQWO\LQYRNHG ,GHDRQO\FRPSLOHWKHPHWKRGVZKHUHWKH DSSOLFDWLRQVSHQGVDORWRIWLPH ,GHDSHUIRUPDGYDQFHGFRPSLOHU RSWLPL]DWLRQIRUWKHKRWWHVWPHWKRGVVLPSOH RUQRFRPSLOHURSWLPL]DWLRQIRUOHVVKRW PHWKRGV +RZ$GDSWLYH&RPSLOHU:RUNV 6HWWKUHHWKUHVKROGV77 77 (DFKPHWKRGKDVDFRXQWHUWKDWLVLQLWLDOL]HG WR:KH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