Java递减 [英] Decrement in Java

查看:42
本文介绍了Java递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int result = 5;    
result = result--;  
System.out.println(result);  

为什么结果等于5?

推荐答案

这什么也没做:

   result = result--;  

因为 result-会在递减前返回结果的值(与-result 会在递减后返回结果的值相反).由于 = 右侧的部分在 = 之前执行,因此您基本上减少了结果,然后在同一行上将result设置为之前的值.减少,因此在实际中取消.

Because result-- returns the value of result before it is decremented (contrary to --result which returns the value of result after it is decremented). And as the part to the right of = is executed before the =, you basically decrement result and then, on the same line, set result to the value it had before the decrement, thus canceling it in practice.

所以你可以写

   result = --result;  

但是,如果您想减少结果,只需执行

But if you want to decrement result, simply do

result--; 

(或-result; ),但是单独使用它是非常不寻常且不典型的,当您不想使用表达式的结果而只是想使用它时,不要使用它减少)

(or --result; but it's very unusual and atypical to use it on its own, don't do it when you don't want to use the result of the expression but simply want to decrement)

这篇关于Java递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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