Javaã®åç½®ã¤ã³ã¯ãªã¡ã³ãã¨å¾ç½®ã¤ã³ã¯ãªã¡ã³ãã®å é¨å®è£ ãèªã¿ãã
id:E_Mattsanããããã³ã¡ã³ãã§æãã¦ããã ãããã¼ã¸ãé¢ç½ãã§ãã
C++ã®åç½®ã¤ã³ã¯ãªã¡ã³ãã¨å¾ç½®ã¤ã³ã¯ãªã¡ã³ãã®å é¨å®è£ ã«ã¤ãã¦æ¸ããã¦ãã¾ããå ãã¿ã¯ããæ°è¨ç More Effective C++ãã¨ããæ¬ã®ããã§ãã
åç½®ã¨å¾ç½®ã®æ¯ãèããåãã«ããããã«ãå é¨çã«ã¯å¾ç½®ã®å®è£ ã«åç½®ã使ã£ã¦ãããã¨ãã話ããèå³æ·±ãã¨æãã¾ããã
ã§ãJavaã ã¨ã©ããªã®ããæ°ã«ãªãã確èªãããã¨ããã¨ãããè¡ãè©°ã¾ã£ã¦ãã¾ãã¾ããã
åãããªããã¨
ã©ã®ã³ã¼ããèªãã°ããããåããã¾ãããæ¨æºAPIã®å®è£ ãªã®ãï¼ JVMã®ã³ã¼ããªã®ãï¼
ããããã++ãã¨ãã+=ãã¨ãã°ã¬ããããªãã£ï¼grepã®æå¹æ§ãé èªï¼ãä½ããã¦ããã¤ã
ä¸å¿ãJVMã«IINC.javaãããã¾ããããèªãã¹ãã¯ããã§ã¯ãªãæ°ããã¾ãã
ã ã£ã¦ãã¤ã³ã¯ãªã¡ã³ãæ¼ç®ã®æ©æ§ãããããªå¤åºãã®ãã¼ã«ã£ã½ãå ´æã«æ¸ãã¦ãããããªãâ¦ã¨ãï¼
ã³ã³ãã¤ã©ã®åå¥è§£æã§ã++ããå¤å¥ãã¦ãå ´æãè¦ã¤ããã°ããã®ããããã¾ããããããã©ãã³ã¼ãï¼ ã©ãã³ã¼ããã¼ã¼
â¦ã¨ã¾ããæ ããªãæãã§ããä¸å¿ããããã¨ãããã¨ã®ã¡ã¢ãæ¸ãã¾ãã
ãµã³ãã«ã³ã¼ã
次ã®2ã¤ã®ã³ã¼ãã«ã¤ãã¦ãå®è£ ã®å·®ç°ã確èªãã¾ãã
åç½®ã¤ã³ã¯ãªã¡ã³ã
PreIncrement.java
package study.increment; public class PreIncrement { public static void main(String[] args) { int hoge = 10; int moga = ++hoge; } }
å¾ç½®ã¤ã³ã¯ãªã¡ã³ã
PostIncrement.java
package study.increment; public class PostIncrement { public static void main(String[] args) { int hoge = 10; int moga = hoge++; } }
jadã£ã¦ã¿ã
ã¯ã©ã¹ãã¡ã¤ã«ãjadã£ã¦ã¿ã¾ãããã¼ã«ã¯JDï¼http://java.decompiler.free.fr/ï¼ã使ãã¾ãã
åç½®ã¤ã³ã¯ãªã¡ã³ã
ããï¼ å¾ç½®ã«ãªã£ã¦ãâ¦ï¼
package study.increment; public class PreIncrement { public static void main(String[] args) { int hoge = 10; hoge++; int moga = hoge; } }
å¾ç½®ã¤ã³ã¯ãªã¡ã³ã
ãã¡ãã¯ãæ¸ããã³ã¼ããã®ã¾ã¾ã
package study.increment; public class PostIncrement { public static void main(String[] args) { int hoge = 10; int moga = hoge++; } }
å½ä»¤ã³ã¼ããè¦ã¦ã¿ã
ä½ãããåãããªãã®ã§ããã¤ãã³ã¼ãã®å½ä»¤åãè¦ã¦ã¿ã¾ãã
javapã«-cãªãã·ã§ã³ãã¤ãã¦å®è¡ãã¾ãã
åç½®ã¤ã³ã¯ãªã¡ã³ãã®å½ä»¤ã³ã¼ã
>javap -c PreIncrement Compiled from "PreIncrement.java" public class study.increment.PreIncrement extends java.lang.Object{ public study.increment.PreIncrement(); Code: 0: aload_0 1: invokespecial #8; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: bipush 10 2: istore_1 3: iinc 1, 1 6: iload_1 7: istore_2 8: return }
å¾ç½®ã¤ã³ã¯ãªã¡ã³ãã®å½ä»¤ã³ã¼ã
>javap -c PostIncrement Compiled from "PostIncrement.java" public class study.increment.PostIncrement extends java.lang.Object{ public study.increment.PostIncrement(); Code: 0: aload_0 1: invokespecial #8; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: bipush 10 2: istore_1 3: iload_1 4: iinc 1, 1 7: istore_2 8: return }
iload_1ã¨iincã®ä½ç½®ãéã«ãªã£ã¦ãããã¨ï¼ã ãï¼ã¯åããã¾ãã
iload_1ã¯ã1çªç®ã®å±æå¤æ°ããå¤ãåãåºãã¦ã¹ã¿ãã¯ã«ç©ãå½ä»¤ãiincã¯ãintå¤ãå®æ°åå¢æ¸ããå½ä»¤ã§ããã
ã»ã»ã»
C++ã¯å¾ç½®ã®å®è£ ã«åç½®ã使ã£ã¦ããããã©ãJavaã¯éãªã®ãï¼
ããã¨ãããã³ã³ãã¤ã«ãã¼ã«ãããé¡æ¨ããã ããªã®ãï¼
å®éã®å®è£ ã¯ã©ããªã£ã¦ããã®ãï¼
â¦ã¨ããç¹ãã確ããããã§ãããµãããã
ã¡ãªã¿ã«
ãµã³ãã«ã³ã¼ãã§ã¯ãå¤æ°mogaãæªä½¿ç¨ã®ã¾ã¾ã§ããã³ã³ãã¤ã©ãæªä½¿ç¨ã®ã³ã¼ããç¡è¦ããå¯è½æ§ãèããããå ´åãããã¯ãã¾ãè¯ãæ¤è¨¼ç¨ã³ã¼ãã§ã¯ãªãã¨ããã¾ããããããprintlnã¨ã使ãã¨ãå½ç¶PrintStreamã¯ã©ã¹ã¨ãããã¼ãããã¦ãã¾ããè¦ããé¨åï¼ã¤ã³ã¯ãªã¡ã³ãã®å®æ ï¼ãåããã¥ãããªãã®ã§ãçãã¾ããããã¡ãªãããªâ¦
ãã®ã¸ãã®ç¢ºèªã®ä½æ³ãæªçãªã®ã§ãè¦ãããã¨ããã