EDIT 1
DISCLAIMER: I know that +++ not an operator, but the + and ++ operators are without a space. I also know that there is no reason to use this; this question is just out of curiosity.
So, I am interested to know if a space is required between + and ++var in Java.
Here is my test code:
int i = 0; System.out.println(i); i = i +++i; System.out.println(i);
This produces:
0 1
which works as I would expect, just as if there was a space between the first and second + .
Then I tried it with string concatenation:
String s1 = "s " + ++i; System.out.println(s1);
This produces:
s 2
But if the third line is uncommented, the code does not compile with an error:
Problem3.java:13: unexpected type required: variable found : value String s2 = "s " +++i; ^ Problem3.java:13: operator + cannot be applied to <any>,int String s2 = "s " +++i; ^
What causes a difference in behavior between string concatenation and adding an integer?
EDIT 2
As discussed in the question of continuing Abhijit , the rule that people talked about (first you need to parse the larger token ++, to the shorter token ++) was discussed in this presentation , where it seems to be called the Munchy Munchy rule.
chm
source share