查看生成的字节码:
public static void main(String[] args) { int x = 1; int y = 1; int z = 1; int a = 1; int b = 1; x = x + 1; y++; ++z; a += 1; b += 2;}产生(使用
javap -c classname)
0: iconst_11: istore_12: iconst_13: istore_24: iconst_15: istore_36: iconst_17: istore 49: iconst_110: istore 512: iload_113: iconst_114: iadd15: istore_116: iinc 2, 119: iinc 3, 122: iinc 4, 125: iinc 5, 228: return
因此使用(jdk1.6.0_18):
x = x + 1
创造
12: iload_113: iconst_114: iadd15: istore_1
而
y++;++z;a += 1;
全部导致
iinc
但是,在笔记本电脑上进行粗略的性能测试后,两者的运行时间几乎没有差异(有时++ x更快,有时x = x + 1更快),因此我不必担心性能影响。



