This may be the silly question but i have no idea why it is so.I have written following code snippet.
public class Test {
public static void main(String... str)
{
int y = 9;
int z = +++y; //unexpected type required:variable found:value
int w = +-+y; // Not Error
}}
Why +-+y works and +++y Not ?
+++y
is interpreted as the ++
operator followed by +y
.
+y
is as valid as -y
is, but the ++
operator expects a variable to operate on (it cannot increment a value), and +y
is considered a value (an addition operation was performed).
+-+y
as 0 + (0 - (0 + y))
, and it has no increment or decrement operators with in it, so even though the operation transform the whole expression into a value (instead of a variable reference) it has no effect.
In Java, the characters +++
mean ++
, followed by +
, which are two different operators. On the other hand, there is no operator +-
, so the characters +-+
mean +
, then -
, then +
.
If you want to play with these operators, there's also ~
, which is a binary not. You can build arbitrary chains with the operators +
, -
and ~
, as long as they don't contain ++
or --
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With