1

I met this code:

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

I think that the compiler will give an error but the result is 5? What is the pourpose of the last '+'(plus) sign? Is it positive sign that marks that 1 is not negative or? And why Java allow this as a valid statement?

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
Xelian
  • 16,680
  • 25
  • 99
  • 152
  • 1
    Read about post increment and preincrement! – Rahul Tripathi Aug 21 '14 at 16:52
  • Is the question about the `++a+a++` part or just about the `++1`? – tobias_k Aug 21 '14 at 16:53
  • possible duplicate of [explain working of post and pre increment operator in Java](http://stackoverflow.com/questions/2371118/explain-working-of-post-and-pre-increment-operator-in-java) – Rahul Tripathi Aug 21 '14 at 16:53
  • ++a increments a and returns the new value (2). plus a++ (a++ increments and returns the previous value (2)), plus positive 1. Simplified: 2 + 2 + (+1) – Vince Aug 21 '14 at 16:54
  • In C or C++, this would be undefined behavior. In Java, it's just plain ugly. – Fred Larson Aug 21 '14 at 16:56
  • 1
    You can even _chain_ those unary `+` and `-`: `int k = ++a + a++ + + + + - - + - + - - - + + -+ +- 1;` gives `5` if the number of `-` is even, else `3` – tobias_k Aug 21 '14 at 16:58
  • I am not asking about post- and pre-increment operators but for the unary plus at the end. – Xelian Aug 21 '14 at 17:05

3 Answers3

8

The statement is effectively grouped like this:

int k = (++a) + (a++) + (+1);

The +1 is a valid number literal, just like -1, but it is equivalent to simply 1.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
6

What is the pourpose of the last '+'(plus) sign?

I'm assuming you understand what the post- and pre-increments are doing here. As for the last +: you're using the unary + operator (JLS §15.15.3), just as in

int a = +1;
int b = 1 + +1;

And why Java allow this as a valid statement?

That's not really a meaningful question. The correct answer to it is "because the JLS says it's allowed". As for why that's the case, we can only speculate, but it makes sense to incorporate a unary plus for the sake of symmetry with unary minus. Unary plus can also be used for numeric promotion; from the link above:

Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.

At run time, the value of the unary plus expression is the promoted value of the operand.

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
5

Yes, that's just a unary plus operator. Granted, its practical uses are fairly limited, but it's valid Java so the compiler doesn't complain.

With parentheses:

int k = (++a) + (a++) + (+1);

Side note: please never write actual code with these pre/post-increment combinations. :-P

Community
  • 1
  • 1
Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51