如果删除+和++运算符之间的空格会发生什么? [英] What happens if you remove the space between the + and ++ operators?

查看:212
本文介绍了如果删除+和++运算符之间的空格会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑1

免责声明:我知道 +++ 不是真正的运营商,而是 + ++ 运营商没有空间。我也知道没有理由使用它;这个问题只是出于好奇。

DISCLAIMER: I know that +++ is not really an operator but the + and ++ operators without a space. I also know that there's no reason to use this; this question is just out of curiosity.

所以,我有兴趣看看<$ c $之间的空间是多少Java中需要c> + 和 ++ var

So, I'm interested to see if the space between + and ++var is required in Java.

这是我的测试代码:

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

打印出来:

0
1

按我的预期工作,只是好像第一个和第二个 + 之间有空格。

which works as I would expect, just as if there were a space between the first and second +.

然后,我尝试使用字符串连接:

Then, I tried it with string concatenation:

String s1 = "s " + ++i;
System.out.println(s1);
// String s2 = "s " +++i;

打印出来:

s 2

但如果第三行未注释,则代码不会编译,错误:

But if the third line is uncommented, the code does not compile, with the 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's causing the difference in behavior between string concatenation and integer addition?

编辑2

如前所述 Abhijit的后续问题,人们提到的规则(在较短的令牌++之前首先解析较大的令牌++)将在此演示文稿,它似乎被称为Munchy Munchy规则。

As discussed in Abhijit's follow-up question, the rule that people have mentioned (the larger token ++ be parsed first, before the shorter token ++) is discussed in this presentation where it appears to be called the Munchy Munchy rule.

推荐答案

编译器在解析源代码时生成最长的令牌,因此当遇到 +++ 时,它会将其作为 ++ +

Compiler generates longest possible tokens when parsing source, so when it encounters +++, it takes it as ++ +.

所以代码为

a +++ b

将永远是与

(a++) + b

这篇关于如果删除+和++运算符之间的空格会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆