Topic: int i =1; i=i++ 为什么结果是 i =1? |
Print this page |
1.int i =1; i=i++ 为什么结果是 i =1? | Copy to clipboard |
Posted by: xiaopan Posted on: 2003-03-18 15:15 这是JVM中的一个问题,在C和C++中结果是符合语意的2,这是由于在C中编译会把这样的语句 i = i++ 优化成 i++, 而JVM却不会,所以结果是1 |
2.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-18 15:23 i=++i 才是i = 2啊。 |
3.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: rainman] | Copy to clipboard |
Posted by: ditty Posted on: 2003-03-18 15:26 如果你是非计算机专业的人,可以理解;否则,这种问题都问的话,真该好好反省一下自己了! |
4.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: ccic134302 Posted on: 2003-03-18 15:54 如果你是非计算机专业的人的话我告诉你: i++是 i 赋给 i 后在加1;++i是 i 加1后赋给i是2 明白否,好好加油!!! |
5.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: snowbug Posted on: 2003-03-18 23:13 xiaopan wrote: i = i++ is the same as the following set of statements: 1). int temp = i; 2). i++; 3). i = temp; Therefore, the original i value is assigned back to the variable i after (i++) statement. |
6.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: snowbug] | Copy to clipboard |
Posted by: mitnickcbc Posted on: 2003-03-18 23:37 snowbug wrote: that's clear. |
7.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: mitnickcbc] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-19 01:41 Java Source
JVM Assembler code:
|
8.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-19 01:45
|
9.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-19 01:46
|
10.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-19 02:19 指令解释: iconst_1 ; push 1 onto the stack istore_1 ;store integer in local variable 1 iload_1 ;push integer in local variable 1 onto the stack iinc <varnum> <n> ; increments the int held in the local variable <varnum> by <n>. iadd ; Pops two integers from the operand stack, adds them, and pushes the integer result back onto the stack |
11.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: rainman Posted on: 2003-03-19 02:35 i = 1;i = i++ 相当于下面的汇编码 0 iconst_1 ; 把1放到堆栈 1 istore_1 ;从栈顶取出1并存在局部变量1中(也就是变量 i ) 这时候i=1; 2 iload_1 ; 把i的值放到堆栈里去,这时候栈顶为1 3 iinc 1 1 ;把变量1(也就是 i)加1,这时候i = 2咯 6 istore_1 ;呵呵,从栈顶取出1存到i里去,呵呵i又回到1了。 7 return ; 返回。 i = 1; i = i+1 也来看一下: 0 iconst_1 ; stack = [1]; 1 istore_1 ; stack =[] ; i = 1; 2 iload_1 ; stack = [1]; i = 1; 3 iconst_1 ; stack = [1,1] ; i =1; 4 iadd ; stack =[2];i = 1; 5 istore_1 ; stack = [];i =2; 6 return i = 1 ; i=++i 0 iconst_1 ; stack=[1]; 1 istore_1 ; stack=[] ; i =1; 2 iinc 1 1 ; stack=[];i=2; 5 iload_1 ; stack=[2];i=2; 6 istore_1 ; stack=[];i=2; 7 return Done. |
12.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-03-19 21:10 snowbug wrote: i = i++ is the same as the following set of statements: 1). int temp = i; 2). i++; 3). i = temp; Therefore, the original i value is assigned back to the variable i after (i++) statement. 我是这样理解的,因为++的优先级高于=;所以按照snowbug的步骤,既能够满足++的优先级高于=,又能够满足i++的先赋值再加一; |
13.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: scottding Posted on: 2003-03-20 09:19 hoho,解释的这么详细。 |
14.Re:int i =1; i=i++ 为什么结果是 i =1? [Re: xiaopan] | Copy to clipboard |
Posted by: scottding Posted on: 2003-03-20 09:21 不过在C++中,++和--的运算在不同的编译器中得到的结果也是不一样的,不同的编译器的处理不同。在java中,也就一种处理了。对于这种问题,不需要深究了,已经有了太多无意义的讨论了。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |