java - What happens if you remove the space between the + and ++ operators? -


edit 1

disclaimer: i know that +++ is not operator + , ++ operators without space. know there's no reason use this; question out of curiosity.


so, i'm interested see if space between + , ++var required in java.

here test code:

int = 0; system.out.println(i); = +++i; system.out.println(i);   

this prints out:

0 1 

which works expect, if there space between first , second +.

then, tried string concatenation:

string s1 = "s " + ++i; system.out.println(s1); // string s2 = "s " +++i; 

this prints out:

s 2 

but if third line uncommented, code not compile, error:

problem3.java:13: unexpected type required: variable found   : value     string s2 = "s " +++i;                 ^ problem3.java:13: operator + cannot applied <any>,int     string s2 = "s " +++i;                      ^ 

what's causing difference in behavior between string concatenation , integer addition?


edit 2

as discussed in abhijit's follow-up question, rule people have mentioned (the larger token ++ parsed first, before shorter token ++) discussed in this presentation appears called munchy munchy rule.

there no +++ operator. have there postfix ++ operator followed infix + operator. compilation error because postfix ++ can applied variable, , "s " isn't variable.

since mean infix + operator followed prefix ++ operator, need put space in between operators.

actually, should anyway. +++ crime against readability!!!


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -