One plus plus two compiles unexpectedly [duplicate]

[亡魂溺海] 提交于 2020-01-10 20:15:28

问题


So, I expect this not to compile, and it doesn't:

// the two is inc'd, so reduces symbolically to println(int int)
// which is a compile error
System.out.println(1 ++ 2);

But this does:

System.out.println(1 + + 2);   // returns three

What gives? Shouldn't it also not compile?

Also, this question is very hard to search for because of the operators..


回答1:


Java is interpreting the working 1 + + 2 as 1 plus positive 2. See the Unary operator section.




回答2:


From the Specification, on Lexical Translations

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. There is one exception: if lexical translation occurs in a type context (§4.11) and the input stream has two or more consecutive > characters that are followed by a non-> character, then each > character must be translated to the token for the numerical comparison operator >.

(Also known as maximal munch.)

The ++ is interpreted as a postfix increment operator which cannot be applied to an integer literal, thus the compiler error.

While

1 + + 2

each character is interpreted separately. 1 is an integer literal, + is the additive operator, + is the unary plus operator, and 2 is an integer literal. The whole expression is equivalent to

1 + (+2)

which is easier to read.




回答3:


In Java/C++/C ++ is not same as + +. ++/-- are the Increment/Decrement operator. The first case does not work because it does not apply to literals (1 or 2). Even then it would not be a valid statement, Neither 1++ 2 nor 1 ++2 are valid statement in Java. The second example works because it is interpreted as 1 + (+2). The Java lexer ignores white space. ​In the same way this is valid :

1 + + + 2   --> 1 + (+ (+2))

Or

1 + + + + + + + + + + + + + + + + + + + + + + + + + + + 2

​It works only because + is a unary operator. It does not work for strings as below :

"a" + + "b"   // does not work because +"b" is not valid.

Similarly it is not valid with multiplication

1 * * 2       // does not work because *2 is not valid.



回答4:


Sometimes it's easier to see a problem using variables.

Your snippet could be rewritten as:

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

Now, you can easily see that the second + operator is used to indicate a positive value. You could also have written +1 + +2.

The - operator could be used to negate an expression.

+ and - are unary operators.




回答5:


The message is:

Main.java:14: error: ')' expected
        System.out.println(1 ++ 2);
                               ^

The 1 ++ 2 statement is parsed as 1 followed by ++ followed by 2. This is interpreted as 1++ and 2 creating a syntax error (and not an unexpected type error; in fact, you will get the same error if you used variables e.g. i ++ j).


The 1 + + 2 statement on the other hand is parsed as 1 followed by + followed by +2 which compiles as expected. The space between the two operators separates the two operators.



来源:https://stackoverflow.com/questions/27625293/one-plus-plus-two-compiles-unexpectedly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!