为什么Java语句评估会像这样发生? [英] Why java statement evaluation is happening like these ?

查看:90
本文介绍了为什么Java语句评估会像这样发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ? 3so the statement will print false ok :) and the second case

System.out.println(++z == z++);

++z将是4z++将是4,所以您得到了true 所以希望你清除...

++z will be 4 and z++ will be 4 so you got true So hope You Clear...

这篇关于为什么Java语句评估会像这样发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆