Java运算符优先级指南 [英] Java operator precedence guidelines
问题描述
误解 Java运算符优先级是常见问题和微妙错误的来源。我很想知道即使是 Java语言规范 说:建议代码不要严格依赖于此规范。 JLS§15.7首选清除 聪明的 ,这方面有什么有用的指导方针吗?
Misunderstanding Java operator precedence is a source of frequently asked questions and subtle errors. I was intrigued to learn that even the Java Language Specification says, "It is recommended that code not rely crucially on this specification." JLS §15.7 Preferring clear to clever, are there any useful guidelines in this area?
以下是一些资源话题:
- JLS Operators
- JLS Precedence
- Java Glossary
- Princeton
- Oracle Tutorial
- Conversions and Promotions
- Java Operator Precedence
- Evaluation Order and Precedence
- Usenet discussion
欢迎添加或更正。
推荐答案
就真实世界而言,可能公平地说:
As far as the "Real World" is concerned, it's probably fair to say:
- 足够的程序员知道乘法/除法优先于加法/减法,在数学上是惯例
- 几乎没有任何程序员能记住任何其他优先规则
因此,除了 * /
vs + - <的特定情况/ code>,我实际上只是使用括号来明确定义预期的优先级。
So, apart from the specific case of */
vs +-
, I'd really just use brackets to explicitly define the precedence intended.
这篇关于Java运算符优先级指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!