为什么在Java中,后递增的优先级比预递增的优先级高? [英] WHY does post incrementing have higher precedence than pre incrementing in Java?

查看:122
本文介绍了为什么在Java中,后递增的优先级比预递增的优先级高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解Java中的后递增和预递增是如何工作的,并且我已经阅读了其他人关于同一主题的无数其他主题.但!我还没有学习过为什么在Java中,后递增的优先级要比预递增的优先级高.

I understand how post-incrementing and pre-incrementing in Java works, and I've read countless other threads/questions from other people regarding this same topic. BUT! I have yet to learn WHY post-incrementing has higher precedence than pre-incrementing in Java.

有人知道吗?

据我所知,后递增的优先级比预递增的优先级高或低将不会影响程序的结果,因为没有其他一元运算符会干扰后递增/预递增.此外,++不是二进制运算符,因此它不会干扰+,-,*,/和%.因此,..为什么?为什么后增量优先于前增量?

To my knowledge, post-incrementing having higher or lower precedence than pre-incrementing will not affect your programs' results since there aren't other unary operators that could interfere with post/pre-incrementing. Furthermore, ++ isn't a binary operator, so it's not like it interferes with +, -, *, / and %. So therefore.. WHY? WHY does post-incrementing have higher precedence than pre-incrementing?

我的动机是这样的:编写Java的目的是使后递增的优先级比预递增的优先级高,并且我还没有看到结果取决于这些规则而有所不同的任何示例.所以我不明白为什么后期/预增量没有相同的优先级

My motivation is this: Java's been made so that post-incrementing has a higher precedence than pre-incrementing and I haven't yet seen any example where the result differs depending on these rules. So I don't understand why post/pre-incrementing doesn't have the same level of precedence

如果有人能启发我,我将非常高兴! :)

I'd be very happy if anyone could enlighten me! :)

这是一个声明Java优先级规则/级别的官方网站: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Here's one official site that states precedence rules/levels in Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

推荐答案

您需要让Java设计人员知道他们为什么进行该设计调用.

You would need to ask the Java designers to know why they made that design call.

但是我希望它是以下一项或两项:

But I expect that it is one or both of the following:

  • 这只是为了与C和C ++保持一致.
  • 这是因为JLS没有直接指定运算符优先级.相反,它是从指定语法的方式出现的.正如 JLS 15.2 所述:

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

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

换句话说,决定操作符(等)优先级的是Java形式语法的结构,而不是某些优先级表. Java语法反映了规范文档的结构.

In other words, it is the structure of the Java formal grammar that determines operator (etc) precedence, not some precedence table. And the Java grammar mirrors the structure of the specification document.

您在Question中链接到的官方站点"实际上是Oracle Java教程的一部分.它是次要来源. JLS优先.

The "official site" you link to in your Question is actually part of the Oracle Java Tutorial. It is a secondary source. The JLS takes precedence.

另请参阅:

  • C++ - Why does postfix operator++ have higher precedence than prefix operator++?
  • C# - Why does x++ have higher precedence than ++x?
  • Wikipedia page on C/C++ Operators.

在此示例中,运算符优先级将产生明显的变化:

Here is an example where the operator precedence would make an observable difference:

 public static volatile a = 42;

 public void busywork() {
    while (true) {
       ++aa--;   // ... assuming this was valid Java code ....
    }
 }

现在创建2个线程,一个线程调用busywork,第二个线程采样a的值.您应该能够观察到a的不同值集,具体取决于相对的运算符优先级.即{41, 42}使用当前规则.如果发布和预递增和递减的优先级相同,那么无论您看到的是{41, 42}还是{42, 43},它都是特定于编译器的.

Now create 2 threads, one to call busywork, and the second to sample the values of a. You should be able to observe different sets of values for a, depending on the relative operator precedence; i.e. {41, 42} with the current rules. If the precedence was the same for post and pre-increment and decrement, then it would be compiler specific whether you saw {41, 42} or {42, 43}.

(当然,对于编译器规范和编译器实现者,拥有两个有效的解析树将使生活变得更加复杂.)

(And of course, having two valid parse trees is going to make life more complicated for the compiler specification and the compiler implementors.)

最后,上面的示例中的++aa--无效是不重要的,而与优先级无关.递增/递减运算符需要一个<lvalue>并计算一个<rvalue>,并且您不能使上述表达式适合 any 排序或优先级的那些约束.

Finally, it is worth nothing that ++aa-- in the example above is invalid irrespective of the precedence. The increment / decrement operators require an <lvalue> and evaluate an <rvalue>, and you can't make the above expression fit those constraints for any ordering or precedence.

因此,前缀和后缀形式被描述为具有不同的优先级这一事实对程序员没有实用的区别.

Therefore the fact that prefix and postfix forms are described as having different precedence makes no practical difference to programmers.

Java教程的作者也许可以将优先级组和后优先级组简化"为该(非确定性)表中的一组.他们选择不这样做.如果您愿意,可以通过适当的渠道提出更正建议.

这篇关于为什么在Java中,后递增的优先级比预递增的优先级高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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