12
返回列表 发新帖
楼主: zhang_oliver

一个奇怪的问题,请教高手!

[复制链接]
论坛徽章:
0
11#
发表于 2001-12-9 17:00 | 只看该作者
这样子啊……我开始明白了

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
12#
发表于 2001-12-9 19:15 | 只看该作者
最初由 dm071 发布
[B]你的第一问题是java 有bug. k=k++,java实际没有对k进行加1计算.
第二个问题是y=k++,编译器先把k赋值给y,然后对k加1.这个语句,java编译器没产生问题. [/B]


Sorry, I don't think so! Java has no bug on this computation. See below explanation:

k = k++;
First compute k++ computation. the expression returns 0 but k is 1. And next assign the result to k. so k is 0, not 1. check the detail computing steps:
1. Do compute k++, the result is 0 because k++ is a post++ computation.
2. k increase itself after giving result 0. So now k is 1.
3. Finally, do the assignment. Give the result 0 the k. So k is 0 now, not 1!

And how about i = 2; i = (++i) + (i++) ? Please see the below steps! You can get it clear to yourself.
1. i++ has precedence, so the right side will increase i to 3, but returns 2. The left will increase i from 3 to 4. So the equation actually looks like i = 4 + 2, and get 6.

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
13#
发表于 2001-12-9 19:20 | 只看该作者

Re: 我说一句

最初由 sun_job 发布
[B]对于付值语句,应该是从右向左运算,至于++运算,属于单目运算;在前时,先加1再开始其他运算;在后时,先进行一个其他运算,再进行加1。 [/B]


Not right!
The reason why right side i++ is performed first is because i++ has precedence as ++i. So if there is ++i and i++ in one expression, first compute i++, then ++i.

And if 2 sub-expressions have the same precedence, compute them from left to right, not right to left!

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
14#
发表于 2001-12-10 11:24 | 只看该作者

It is not a bug in Java. If you read the Java Language Specification

carefully, this behavior is clearly stated in the spec. In other words, which ever JVM you use, you'll always get this result. In fact, this is a bug in C, because there is no definition of this behavior, so the result depends on your choice of C compiler.

使用道具 举报

回复
论坛徽章:
0
15#
发表于 2001-12-19 13:14 | 只看该作者
这个问题可能是这个原因,不能说实bug
int a=0,b=0;
                    --------------
         a----->  |     0      |   
                    --------------
a=a++;此时先做了一次内存拷贝,a指向了新的内存,而后的a++改变的是原来的内存。

                    --------------
                   |     1      |   
                    --------------

                    --------------
         a----->  |     0      |   
                    --------------

这也没什么不正确的,只是大家习惯了c 、c++而已。

这与
b=a其实是一样的,=相当于一个拷贝函数,先进行了内存拷贝---tmp ,然后b指向tmp的内存

java的这种方式可能性能不太好,但是只是对简单类型而已

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
16#
发表于 2001-12-20 13:46 | 只看该作者
就这个问题
我曾经在某个JAVA论坛和人吵了一架

但是始终认为是某些C有问题,而不是JAVA的问题!!

使用道具 举报

回复
论坛徽章:
0
17#
发表于 2001-12-21 16:14 | 只看该作者

如此解释

int k=0;
k=k++;
k=k++;
System.out.println("the first k="+k);
int h=k;
System.out.println("the first h="+h);
k++;
System.out.println("the second k="+k);
程序执行过程如下:
第一句:int k=0; 执行完后k的值为0;
第二局:k=k++;  先执行(k++)=0,然后k值加1,最后将前边(k++)的值(为0)赋给k(此时k值为1),因此最终结果是k值为0。也就是说实际上执行的是:1(k):=0(k++);
第三句:同第二句,结果是k值为0;
第四句:System.out.println("the first k="+k); 结果为the first k=0;
第五句:int h=k; 结果是h值为0;
第六句:System.out.println("the first h="+h); 结果为the first h=0;
第七句:k++; 结果是k值加1;
第八句:System.out.println("the second k="+k); 结果为:the second k=1;
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);
程序执行过程如下:
第一句:int x=0,y=0; 执行完后x的值为0,y的值为0;
第二局:y=x++; 先执行(x++)=0,然后x值加1,最后将前边(x++)的值(为0)赋给y(此时y值为0),因此最终结果是y值为0,x值为1;
第三、四句:System.out.println("the first x="+x);
System.out.println("the first y="+y);
            结果为the first x=1
                  the first y=0;
第五句:x++; x的值加1,结果x值为2;
第六句:System.out.println("the first x="+x); 结果为the first x=2;

使用道具 举报

回复
论坛徽章:
0
18#
发表于 2001-12-21 16:34 | 只看该作者
int i=0;
i=i++;
System.out.println("i="+i);
……
在C++中确实输出1,而在java中输出是0,这是c++的BUG,因为"="号的优先级比"++"号底,应先运算"++"号。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表