Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 ?

like image 839
Prashant Shilimkar Avatar asked Dec 26 '22 18:12

Prashant Shilimkar


2 Answers

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

like image 192
Gilad Naaman Avatar answered Dec 28 '22 09:12

Gilad Naaman


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

like image 44
Roland Illig Avatar answered Dec 28 '22 10:12

Roland Illig