说说Java中的 + 运算符 | bit-knowledge
局部变量中的 + (代码1) public void process() { int a = 0; a = a + 1; // (1) a += 1; // (2) a++; // (3) } 代码1中,(1), (2), (3)都实现对变量 a 的加1操作,但是细节方面有细微差异。我们看下编译后的代码: public void process(); Code: // int a = 0; 0: iconst_0 1: istore_1 // a = a + 1; 2: iload_1 3: iconst_1 4: iadd 5: istore_1 // a += 1; 6: iinc 1, 1 // a++; 9: iinc 1, 1 12: return 分析反编译代码我们可以发现, 而 所以实际编码中用 实例变量中的 + (代码2) public class Plus { private int a = 0; public void process() { a = a + 1; // (1) a += 1; // (2) a++; // (3) } } 编译后的字节码 public void process(); Code: // a = a + 1; // stack[this] 0: aload_0 // stack[this, this] 1: aload_0 // stack[this, a] 2: getfield #2 // Field a:I // stack[this, a, 1] 5: iconst_1 // stack[this, a + 1] 6: iadd // stack[] this.a = a + 1 7: putfield #2 // Field a:I // a += 1; // stack[this] 10: aload_0 // stack[this, this] 11: dup // stack[this, a] 12: getfield #2 // Field a:I // stack[this, a,Read full article from 说说Java中的 + 运算符 | bit-knowledge
No comments:
Post a Comment