Prashant Shilimkar
Prashant Shilimkar

Reputation: 8840

+++x unexpected type required: variable found: value

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 ?

Upvotes: 4

Views: 463

Answers (2)

Roland Illig
Roland Illig

Reputation: 41676

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 --.

Upvotes: 3

Gilad Naaman
Gilad Naaman

Reputation: 6550

+++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.

Upvotes: 7

Related Questions