Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does different spacing affect the unary operator?

Can anyone explain me how different spacing affects the unary operator?

int i = 1; int j = i+ + +i; // this will print j=2 int k = i++ +i; // this will print k=3 int l = i+++i; // this will print l=3 int m = i++++i; // compile time error 

.

like image 356
Ravi Godara Avatar asked Feb 25 '14 09:02

Ravi Godara


People also ask

Is an unary operator and change 1 to 0 and 0 to 1?

Sometimes a unary operation is called a monadic operation or a singulary operation. In Boolean algebra , there is only one unary operation, known as negation. This operation changes the value of the bit (binary digit) from 0 to 1 or from 1 to 0.

Which is the correct example of a unary operator?

Which is the correct example of a unary operator? Explanation: &, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1.

Is addition a unary operation?

Trigonometry. , can be seen as unary operations. This is because it is possible to provide only one term as input for these functions and retrieve a result. By contrast, binary operations, such as addition, require two different terms to compute a result.


2 Answers

First, let's separate this into three separate cases which can't interact:

int i = 1; System.out.println(i+ + +i); // 2  int j = 1; System.out.println(j++ +j); // 3  int k = 1; System.out.println(k+++k); // 3 

Now let's rewrite them using brackets:

int i = 1; System.out.println(i + (+(+i)));  int j = 1; System.out.println((j++) + j);  int k = 1; System.out.println((k++) + k); 

First operation

Here we can't be using the prefix or postfix ++ operators, as we don't have a token of ++ anywhere. Instead, we have a binary + operator and two unary + operators.

Second operation

This one's simple: it's pretty much as it reads, a postfix ++ operator followed by a binary + operator (not the unary + operator that +j might otherwise imply).

Third operation

The final line is parsed as (k++) + k rather than k + (++k). Both will actually give the same answer in this situation, but we can prove which is which by using two different variables instead:

int k1 = 1; int k2 = 1; System.out.println(k1+++k2); // Prints 2 System.out.println(k1); // Prints 2 System.out.println(k2); // Prints 1 

As you can see, it's k1 that's been incremented rather than k2.

The reason that k+++k is parsed as tokens of k, ++, +, k is due to section 3.2 of the JLS, which includes:

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would.

Fourth operation (compile failure)

The same "longest possible translation" rule parses i++++i as i, ++ ,++, i which isn't a valid expression (because the result of the ++ operation is a value, not a variable).

like image 197
Jon Skeet Avatar answered Sep 28 '22 19:09

Jon Skeet


+ is an operator, and ++ is an operator, but + + is not - + + is interpreted as two +s, not one ++. So the space forces your code to be interpreted differently.

+ is both a binary operator which adds two numbers and a unary operator which does not change a number (it exists only for consistency with the unary - operator).

If we use add instead of binary +, no-change instead of unary +, and increment instead of ++ then it might be more clear.

int j=i+ + +i becomes int j = i add no-change no-change i;.
int k=i++ +i; becomes int k=i increment add i;.
I suspect int k = i+++i; also becomes int k = i increment add i; but I have not checked this with the language specification.

like image 43
user253751 Avatar answered Sep 28 '22 19:09

user253751