箭头(->)运算符的优先级/优先级最低,还是分配/组合分配的优先级最低? [英] Arrow (->) operator precedence/priority is lowest, or priority of assignment/combined assignment is lowest?

查看:477
本文介绍了箭头(->)运算符的优先级/优先级最低,还是分配/组合分配的优先级最低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JLS :

最低优先级运算符是lambda表达式的箭头 (->)紧随其后的是.

The lowest precedence operator is the arrow of a lambda expression (->), followed by the assignment operators.

遵循哪个方向(优先级升高,优先级降低)? -跟随"是指分配具有更高的优先级还是更低的优先级(相对于箭头运算符)?我猜想,在增加方面,因为最低"(箭头)表示绝对最低.

Followed in which direction (increasing priority, decreasing priority)? - "followed" means assignment has higher priority or lower priority (with respect to arrow operator)? I guess, in increasing, because "lowest" (for arrow) means absolutely lowest.

据我了解,箭头(->)应该位于此 Princeton 运算符优先级表(位于所有赋值运算符的下面),因此,箭头(->)的优先级为0(零)(按照该表).

As I understand, arrow (->) should be at the very bottom of this Princeton operator precedence table (that is below all assignment operators), thus arrow (->) having 0 (zero) priority Level (as per that table).

我的理解正确吗?

ExamTray 似乎在说该箭头的优先级至少与分配相同.此外,还阐明了箭头的关联性是Left-> To-> Right(与分配不同).我没有找到JLS的箭头关联性报价.

ExamTray seems to say that arrow priority is at least same as assignment... Plus clarified that arrow associativity is Left->To->Right (unlike assignment). I didn't find any JLS quote for arrow associativity.

出于种种原因,我一直认为分配优先级原则上是最低的.

I always used to think that assignment priority is principally lowest for a reason.

推荐答案

请注意在

运算符之间的优先级由语法产生的层次结构管理.

Precedence among operators is managed by a hierarchy of grammar productions.

Java语言的语法确定了可能的构造,并且隐式地确定了运算符的优先级.

The grammar of the Java language determines which constructs are possible and implicitly, the operator precedence.

即使 princeton表,您也已链接了以下状态:

Even the princeton table you’ve linked states:

Java语言规范中没有显式的运算符优先级表.网络和教科书中的不同表格在某些微小的方面存在分歧.

There is no explicit operator precedence table in the Java Language Specification. Different tables on the web and in textbooks disagree in some minor ways.

因此,Java语言的语法不允许在赋值运算符的左侧使用lambda表达式,同样,也不允许在->左侧的赋值.因此,尽管这些操作符之间没有歧义,但优先规则(尽管在JLS中已明确指出)变得毫无意义.

So, the grammar of the Java language doesn’t allow lambda expressions to the left of an assignment operator and likewise, doesn’t allow assignments to the left of the ->. So there’s no ambiguity between these operators possible and the precedence rule, though explicitly stated in the JLS, becomes meaningless.

这允许编译,例如这样的宝石,毫不含糊:

This allows to compile, e.g. such a gem, without ambiguity:

static Consumer<String> C;
static String S;
public static void main(String[] args)
{
  Runnable r;
  r = () -> C = s -> S = s;
}

这篇关于箭头(-&gt;)运算符的优先级/优先级最低,还是分配/组合分配的优先级最低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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