为什么Java语句评估会像这样发生? [英] Why java statement evaluation is happening like these ?
问题描述
int z = 1;
System.out.println(z++ == ++z);
System.out.println(++z == z++);
输出将是:
false
true
我不明白为什么,请给我解释一下.
and I don't get why, please explain this to me.
推荐答案
首先,您应该清除有关预增量&的信息.后期递增 让我们看看
First You Should Clear About Pre Increment & Post Increment Lets Have A Look
int i=1;
int j=i++;
int k=++i;
此处j
的值将是1
,而k
的值将是3
here in the value of j
will be 1
and k
will be 3
为什么在i++
中意味着首先将值i
存储到j
中,然后将i
的值递增,所以
why means in i++
first store the value i
into j
and after which increments the value of i
so
j=1,i=2 ok :)
现在是++i
的情况,首先将值递增,然后将其存储到k
中
now in the case of ++i
where first increment the value and after that store into k
so
k=3,i=3 ok :)
现在要问您的问题
int z = 1; System.out.println(z++ == ++z);
所以告诉我z++
是什么? 1 OK :)
因此++z
将是? 3
,因此该语句将打印false
好的:)
第二种情况
so tell me what will be z++
? 1 ok:)
hence ++z
will be ? 3
so the statement will print false
ok :)
and the second case
System.out.println(++z == z++);
++z
将是4
,z++
将是4
,所以您得到了true
所以希望你清除...
++z
will be 4
and z++
will be 4
so you got true
So hope You Clear...
这篇关于为什么Java语句评估会像这样发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!