Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 int i =1; i=i++ 为什么结果是 i =1?
xiaopan





发贴: 71
积分: 20
于 2003-03-18 15:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这是JVM中的一个问题,在C和C++中结果是符合语意的2,这是由于在C中编译会把这样的语句 i = i++ 优化成 i++, 而JVM却不会,所以结果是1



SWT Designer v1.2.3 Released!
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-18 15:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
i=++i 才是i = 2啊。




JBuilder使用技巧
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:rainman]
ditty

负资产小资

CJSDN高级会员


发贴: 1038
积分: 143
于 2003-03-18 15:26 user profilesend a private message to usersend email to dittysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果你是非计算机专业的人,可以理解;否则,这种问题都问的话,真该好好反省一下自己了!


内忧外患的时代,洗心革面,阿咪豆腐~

Hibernate的优点
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
ccic134302





发贴: 185
积分: 40
于 2003-03-18 15:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果你是非计算机专业的人的话我告诉你:
i++是 i 赋给 i 后在加1;++i是 i 加1后赋给i是2
明白否,好好加油!!!




学java的朋友,请一起来投票让sun推出中文jdk文档!!!
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
snowbug



CJSDN高级会员


发贴: 418
积分: 130
于 2003-03-18 23:13 user profilesend a private message to usersend email to snowbugsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
xiaopan wrote:
这是JVM中的一个问题,在C和C++中结果是符合语意的2,这是由于在C中编译会把这样的语句 i = i++ 优化成 i++, 而JVM却不会,所以结果是1

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.




关于 异常 。。。。 谢谢指教
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:snowbug]
mitnickcbc





发贴: 165
积分: 60
于 2003-03-18 23:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.


that's clear.




从Coding Fan到真正的技术专家
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:mitnickcbc]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-19 01:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Java Source

public class Test {
  public static void main(String[] args) {
    int i=1;
    i = i++;
  }
}


JVM Assembler code:

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 <Method java.lang.Object()>
4 return


Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iload_1
3 iinc 1 1
6 istore_1
7 return






致JAVA初学者+如何下手学JAVA
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-19 01:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

public class Test {
  public static void main(String[] args) {
    int i=1;    
    i = i+1;
  }
}



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 <Method java.lang.Object()>
4 return

Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iload_1
3 iconst_1
4 iadd
5 istore_1
6 return






一个程序员的奋斗历程
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-19 01:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

public class Test {
  public static void main(String[] args) {
    int i=1;    
    i = ++i;
  }
}



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 <Method java.lang.Object()>
4 return

Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iinc 1 1
5 iload_1
6 istore_1
7 return






SWT Designer v1.2.3 Released!
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-19 02:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
指令解释:

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





CJSDNer6月14日北京聚会花絮
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
rainman

阿熊

元老


发贴: 5644
积分: 454
于 2003-03-19 02:35 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.





联想游戏(欢迎跟贴,哈)
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
cmslovehxh



发贴: 0
积分: 0
于 2003-03-19 21:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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++的先赋值再加一;




一个程序员的奋斗历程
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-03-20 09:19 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hoho,解释的这么详细。


Blog   Home   @unumu


Hibernate开发指南
作者 Re:int i =1; i=i++ 为什么结果是 i =1? [Re:xiaopan]
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-03-20 09:21 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不过在C++中,++和--的运算在不同的编译器中得到的结果也是不一样的,不同的编译器的处理不同。在java中,也就一种处理了。对于这种问题,不需要深究了,已经有了太多无意义的讨论了。


Blog   Home   @unumu


Eclipse使用技巧

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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