对于++和--这两个运算符,c/c++语言和Java语言有着截然不同的结果。大家可以测试一下。看看下面的程序:
…… public static void main(java.lang.String[] args) {
int k=0; k=k++; System.out.println("the first k="+k);//0???? k++; System.out.println("the second k="+k); ///////////////////////////////////////////// int x=0,y=0; y=x++; System.out.println("the first x="+x); System.out.println("the first y="+y); x++; System.out.println("the first x="+x); } …… 在c/c++中,上述程序的输出结果为: the first k=1 the second k=2 the first x=1 the first y=1 the second x=2 而在java中结果则大出所有人的意料,结果为: the first k=0 the second k=1 the first x=1 the first y=0 the first x=2 为什么呢?下面看看网友们的解答,从中我们可以看看大家对这个问题的理解:
【发言1:】 k=k++; // 等价于:k=k ; k=k+1; y=x++; // 等价于:y=x ; x=x+1;
System.out.println(--j); // 等价于:System.out.println( j ); j=j+1;
while(i-->0){ ... //等价于 :while(i>0) { i=i+1; .....
这是和 c 一样的基本语法
【发言2:】 k=k++; // 等价于:k=k ; k=k+1; y=x++; // 等价于:y=x ; x=x+1; System.out.println(--j); // 等价于:System.out.println( j ); j=j-1; while(i-->0){ ... //等价于 :while(i>0) { i=i-1; .....
【发言3:】 不对,不对 显然当 k = k ++时,k++没有作用,只相当于k=k !!! java怎么会这么处理呢? 值得研究一下!
【发言4:】 据同事说在c语言里,以下几句的输出结果: …… int k=0; k=k++; System.out.println("the first k="+k);//0???? k++; System.out.println("the second k="+k); …… 结果: the first k=1 the second k=1 这个结果比较好理解一点,可是java中的就不知道该如何理解了。
【发言5:】 java里面好象k=k++就等于k=k。
【发言6:】 k=k++ 对于++这种表达式,并没有规定++运算是发生在哪一个时刻的,只规定是在取K值之后运算结束之前。我想现在是这样: 首先取出K的值,此时为0,然后执行++运算,这时K=1,然后再执行=运算,将刚才取出的值“0”赋给K,相当于K=0。 所以运算结束后K应该为0 出现这样的结果原因我想主要就是没有规定++运算具体是在哪个时刻执行。 其实似乎我看书的时候也提倡不要用这种有了++--运算之后还会参加其它运算的表达式,如:B=K++ + K++
【发言7:】 好象在C语言中与JAVA中++与--不一样吧,我以前做过试验,同样的语句在C中与在JAVA中 的结果就是不一样的
【发言8:】 k = k++确实是一条无用的语句 你可以试试 k += 1 我个人认为对k赋值还是用赋值运算符比较好
【发言9:】 你为什么要使用k=k++这样的语句呢?你是想让k加一还是保留原值呢?保留原值就不要写 任何代码;想让k加一就用k++。
i=j++; 等价于
k=j; j=j+1; i=k;
假如把i=j++中的i换成j,则j=j++ k=j; j=j+1; j=k; 结果就是j=j
【发言10:】 k=k++; 等价于:k=k+1 吗?, 我觉得不等于。 似乎应该这样 >>: i=i++ if: i=0; then: (i++)的值为:0;
0付给i 所以 i=0
++ 发生在付值之前 不影响结果
【最终的解释:】 What happens is that the initial value of x is stored in a temporary register, then x is incremented, then the value stored in the register is asigned to the left hand side of the eXPression, in this case the LHS is x so x gets its original value. int x = 1; x = x++; Steps: 1 initial value of x is stored in temp register. So temp = 1. 2 x is incremented. x = 2 and temp = 1. 3 the value of the temp register is assigned to the LHS. x = 1
【中国Java联盟网友SuperMMX的解释:】 关于后 ++ 运算
By SuperMMX
以下所指的 ++ 运算都指后 ++ 运算.
int x = 0; x = x++;
x 的值在 java 和 c 中的不同.
看以下程序:
Test.java
public class Test { public static void main(String[] args) { int i = 0; i = i ++; System.err.println(i); System.exit(0); } }
javap -c Test
Compiled from Test.java public class Test extends java.lang.Object { public Test(); public static void main(java.lang.String[]); }
Method Test() 0 aload_0 1 invokespecial #1 4 return
Method void main(java.lang.String[]) 0 iconst_0 1 istore_1 2 iload_1 3 iinc 1 1 6 istore_1 7 getstatic #2 10 iload_1 11 invokevirtual #3 14 iconst_0 15 invokestatic #4 18 return
前面是 javap 反编译出来的字节码. 我们所关心的是 main 中 0 - 10 行.
首先我们必须对 Java 虚拟机的机制有一些了解, java 的虚拟机主要是基于 栈的一种模拟机. 通常有这样几种结构: 累加器结构, 通用寄存器结构, 存储器-存储器结构, 栈结构. 这里只说明一下栈结构的工作方式.
栈顾名思义, 就是先进后出, 只能对栈顶元素进行操作. 可能的操作比如进栈, 出栈, +, - *, / 等等. 举个简单的例子来说, 下面的伪指令:
push a push b add pop c
栈的变化是这样的:
b ___ _a_ _a_ _a+b_ ___
假如 a = 1, b = 2, 那么 c 最后等于 3.
Java 中的指令非常多, 不过也就是这些基本的东西所组成. 我们来看一下以上指令 0 iconst_0 常数 0 进栈 1 istore_1 把栈顶元素 pop 出来, 存储在 #1 变量中 (也就是 i) 2 iload_1 i 的值进栈 3 iinc 1 1 将 #1 (i) 加 1, 前一个 1 是变量的 index, 后面的 1 是增加的常量 6 istore_1 把栈顶元素 pop 出来, 存储在 #1 变量中(i) 7 getstatic #2 10 iload_1 11 invokevirtual #3
从上面的解释我们可以看出, i 最终的值是 0.
相应的 c 程序如下:
#include
int main() { int i = 0; i = i ++; printf("%d
", i ); return 0; }
0x8048440 : push %ebp 0x8048441 : mov %esp,%ebp 0x8048443 : sub $0x18,%esp 0x8048446 : movl $0x0,0xfffffffc(%ebp) 0x804844d : mov 0xfffffffc(%ebp),%eax 0x8048450 : mov %eax,0xfffffffc(%ebp) 0x8048453 : incl 0xfffffffc(%ebp) 0x8048456 : add $0xfffffff8,%esp 0x8048459 : mov 0xfffffffc(%ebp),%eax 0x804845c : push %eax 0x804845d : push $0x80484d4 0x8048462 : call 0x8048344 0x8048467 : add $0x10,%esp 0x804846a : xor %eax,%eax 0x804846c : jmp 0x8048470 0x804846e : mov %esi,%esi 0x8048470 : leave 0x8048471 : ret
最重要的是 main + 6 到 main + 19 部分. 也是我们所关心的部分.
: movl $0x0,0xfffffffc(%ebp) 给 i 赋值为 0 : mov 0xfffffffc(%ebp),%eax 把 i 的值赋给 eax 寄存器 : mov %eax,0xfffffffc(%ebp) 把 eax 寄存器的值赋给 i (计算完毕, 没有其他计算) : incl 0xfffffffc(%ebp) 将 i 加 1.
从中可以看出, i 最后的值是 1;
到现在为止, 我们粗略看了一下 Java 和 C 中对于 ++ 运算的不同. 但是还并不明显.
我们再看一个例子:
public class Test { public static void main(String[] args) { int i = 1, j = 2; i = (j ++) + (i ++); System.err.println(i); System.exit(0); } }
0 iconst_1 1 istore_1 2 iconst_2 3 istore_2 4 iload_2 5 iinc 2 1 8 iload_1 9 iinc 1 1 12 iadd 13 istore_1
#include
int main() { int i = 1, j = 2; i = (j++) + (i++); printf("%d
", i ); return 0; }
0x8048446 : movl $0x1,0xfffffffc(%ebp) 0x804844d : movl $0x2,0xfffffff8(%ebp) 0x8048454 : mov 0xfffffff8(%ebp),%eax 0x8048457 : mov 0xfffffffc(%ebp),%edx 0x804845a : lea (%edx,%eax,1),%ecx 0x804845d : mov %ecx,0xfffffffc(%ebp) 0x8048460 : incl 0xfffffffc(%ebp) 0x8048463 : incl 0xfffffff8(%ebp)
从上面的例子我们可以看出, 在 Java 和 C 中, ++ 运算的语义是有所不同的, Java 中 ++ 运算是在赋值运算之前完成的, 而 C 中是在赋值预算之后完成的.
测试环境:
Debian 2.2r3 blackdown jdk 1.3.1 gcc 2.95.4 gdb GNU gdb 5.0.90-cvs (MI_OUT)
|