int i = 0;
boolean b = true;
System.out.println(b && !(i++ > 0))
When I compile the above code I get a value true back.
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)
which should return true
. Then the statement would be true && false
, which is false
.
What am I missing?
Best Answer
Java behaving correctly :)
That is postfix increment.
It generated result and then incremented that value later.
i++
will use the previousvalue
ofi
and then it willincrement
it.When you use ++ ,it's like
language specification on Postfix Increment Operator ++
Possible solution would be
++i
, which is as per your requirment,