Re: [問題] java byte code問題

看板java作者 (十年一夢)時間9年前 (2014/12/29 20:52), 9年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《kdok123 (小天)》之銘言: : http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings : 連結為java bytecode instruction listings : 有個三個疑問想問: : getstatic : : get a static field value of a class, where the field is identified by field : reference in the constant pool index (index1 << 8 + index2) : 後面括號index1 << 8 + index2 是什麼意思? getstatic instruction 後面有接著兩個 bytes 的 index,暫時以 index1, index2 稱這兩個 bytes,括號部分為 interpret 這兩個 bytes 為一個 index 數值的方式。 (換句話說把兩個 byte 皆以 unsigned int 解讀為 index1, index2,則 field ref 在 constant pool 的 index 為 index1 * 2^8 + index2。 : invokespecial : : invoke instance method on object objectref, where the method is identified by : method reference index in constant pool (indexbyte1 << 8 + indexbyte2) : 這邊的objectref 是什麼意思? : 每次只要new的時候都會有這行,是什麼原因?(跟instance method的關係?) 在 JVM stack 裡的數值扣除掉 int/long/float/double 這些 primitive type,剩的 都是 objectref(你可以看成 JAVA PL 裡的 reference value)。 Java new operator 的作用包含 object allocation 與 object initialization, 當 Java 程式碼裡出現有出現 new SomeClass(...) 的 expression,編譯產出的 bytecode 就會包含一個 new instruction(object allocation),以及一個 invokespecial instruction 來執行該 object 所屬 class 所定義的某個 constructor(object initialization)。 : invokevirtual: : invoke virtual method on object objectref, where the method is identified by : method reference index in constant pool (indexbyte1 << 8 + indexbyte2) : 每次call method的時候也會產生這行,連自定義的method也是(自定義的method跟 : virtual method有什麼關係?) : 請教大家了 在 Java PL 中,扣掉 static method、constructor 與 private method,其他的 method 都是 virtual function(method)。 invoke static method 是透過 invokestatic instruction,invoke constructor、 private method 與 overriden method 是 invokespecial instruction,其他的 method 被調用都是使用 invokevirtual instruction(假如 objectref 是某種 interface,而被調用 method 有定義在此 interface 裡,那麼使用的 instruction 是 invokeinterface)。 class Bar { public void foobar() {...} } class Foo implements Runnable { private void runImpl() {...} public void run() { runImpl(); // invokespecial } public static Foo create() { return new Foo(); // invokespecial for Foo() constructor } public void foobar() { super.foobar(); // invokespecial } public static void main(String[] args) { Foo a = create(); // invokestatic Runnable r = a; a.run(); // invokevirtual r.run(); // invokeinterface } *Java 1.7(Java 7) 有引進一個新的 invokedynamic instruction。這部分可以 暫時先略過,以後再看看。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.39.248.222 ※ 文章網址: http://www.ptt.cc/bbs/java/M.1419857579.A.82E.html ※ 編輯: sbrhsieh (114.39.248.222), 12/29/2014 21:53:02

12/30 11:09, , 1F
謝謝你! 好厲害,寫的真清楚! 感謝!!
12/30 11:09, 1F
文章代碼(AID): #1KeKwhWk (java)
文章代碼(AID): #1KeKwhWk (java)