关于Postfix Increment Operator ++的澄清:java [英] Clarification regarding Postfix Increment Operator ++ :java

查看:123
本文介绍了关于Postfix Increment Operator ++的澄清:java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i = 0;
boolean b = true;
System.out.println(b && !(i++ > 0))

当我编译上面的代码时,我得到一个值true。

When I compile the above code I get a value true back.

但是怎么可能,因为参数的第二部分(因为b已经是真的)基本上翻译为

But how can that be, since the second part of the argument (since b is true already) basically translates to

(0 + 1> 0)=> (1> 0)

应返回 true 。然后声明将是 true&& false false

which should return true. Then the statement would be true && false, which is false.

我缺少什么?

推荐答案

Java行为正确:)

Java behaving correctly :)

i++

后缀增量。

它生成结果,然后稍后递增该值。

It generated result and then incremented that value later.

!(i++ > 0) // now  value is still zero

i ++ 将使用 i的前一个 然后它将增加它。

当你使用++时,它就像

When you use ++ ,it's like

temp=i;
i += 1; 
i=temp;     // here old value of i.

Postfix Increment Operator ++上的语言规范


将值1添加到变量的值中,并将总和存储回变量中。 ......

the value 1 is added to the value of the variable and the sum is stored back into the variable. ......

后缀增量表达式的值是存储新值之前变量的值。

The value of the postfix increment expression is the value of the variable before the new value is stored.

可能的解决方案是 ++ i ,这是根据您的要求,

Possible solution would be ++i, which is as per your requirment,


前缀增量运算符++

前缀增量表达式的值是存储新值后变量的值。

The value of the prefix increment expression is the value of the variable after the new value is stored.

这篇关于关于Postfix Increment Operator ++的澄清:java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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