简体   繁体   English

Java中算术运算符的优先级

[英]Precedence of arithmetic operators in Java

I was reading "Programming in Java Deitel & Deitel 7th Edition" and I watched the precedence of operators but I was with some trepidation. 我正在阅读“ Java Deitel和Deitel第7版中的编程”,并且看到了运算符的优先级,但是我有些担心。

The author says that when there is a mathematical typical expression in which there are operators of equal precedence operation runs from left to right. 作者说,当存在一个数学上的典型表达式时,其中有相等优先级的运算符从左到右运行。 For example: 例如:

System.out.println (a + b + c - d);

In this sense the operation is executed from left to right, then I research on the internet and I find that the "=" operator runs from right to left. 从这个意义上说,该操作是从左到右执行的,然后我在互联网上进行了研究,发现“ =”运算符从右到左运行。 For example: 例如:

int x;
x = 4 * 4 * 4% 2;

All taxes fine until I ponogo to investigate further and write code and it occurs to me this, I wrote this exeption: 所有税款都可以罚款,直到我Ponogo进一步调查并编写代码,这在我看来是由我写的:

System.out.println ((4 * 4) + (8 * 8) * (4 * 4) - 16/4);

And my question is this, if the book says that the addition and subtraction have lower priority compared to other operators, What if I add the value of a typical expression multiplied ?. 我的问题是,如果这本书说加法和减法的优先级比其他运算符低,那么如果我将一个典型表达式的值相乘怎么办? Let me explain: 让我解释:

When I put (4 * 4) + (8 * 8) * (4 * 4) I am adding the value of the multiplication, then lower the precedence of the operator? 当我放(4 * 4) + (8 * 8) * (4 * 4)我要加上乘法的值,然后降低运算符的优先级?

Another question: 另一个问题:

If I have an example as follows: 如果我有一个如下示例:

System.out.println (a * b * c + d + e + f * g / h);

As higher precedence operations are executed first order from left to right, when I get the time to run the higher precedence Does it run from left to right as well? 由于较高的优先级操作是从左到右执行的,因此,当我有时间运行较高的优先级时,它也从左至右运行吗?

When the value of a variable is assigned, Do operators of higher precedence associate from right to left? 分配变量的值时,优先级较高的运算符是否从右到左关联?

I'll try to answer this with some references, so that you can go into whatever depth you want. 我将尝试通过一些参考文献来回答这个问题,以便您可以随意深入。 It's not entirely clear what is confusing you. 目前尚不清楚什么使您感到困惑。

Firstly, you need to be clear on the difference between assignment operators ( = in your examples, but these also include eg += etc) and other (binary) operators (eg + , - , * , % etc). 首先,您需要弄清楚赋值运算符(在您的示例中为= ,但其中还包括+=等)与其他(二进制)运算符(例如+-*%等)之间的区别。 Assignment operators (such as = ) evaluate right to left - ie they work out the value of everything on the right of the operator and then assign it to whatever is on the left. 赋值运算符(例如= )从右到左求值-即,他们计算出运算符右边的所有值, 然后将其赋给左边的任何值。

For other operators, they first apply operator precedence and then apply left to right operation on operators with equal precedence. 对于其他运算符,它们首先应用运算符优先级, 然后对具有相同优先级的运算符应用从左到右的运算。 A table of precedence and discussion of this is available in the official tutorial . 官方教程中提供了优先级表和对此的讨论。 It is very similar to the table (Fig A.1) on p.1509 of the book you are using, but the web tutorial explanation may be easier to understand. 它与您使用的书的第1509页的表格(图A.1)非常相似,但是网络教程的说明可能更容易理解。 It can be summed up as precedence being applied as expressions in brackets first, followed by postfix then unary operators (eg x++ , then -x ), then multiplicative operators ( * , \\ , % ) and then additive ( + , - ) and finally lots of logical operators that are not directly relevant to this question. 可以将其总结为优先级,首先在括号中将其用作表达式,然后是后缀,然后是一元运算符(例如x++ ,然后是-x ),然后是乘法运算符( *\\% ),然后是加法运算符( +- )与这个问题没有直接关系的许多逻辑运算符。

Finally, the tutorial contains the statement 最后,本教程包含以下语句

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. 当具有相同优先级的运算符出现在同一表达式中时,必须控制一个规则,然后首先评估哪个规则。 All binary operators except for the assignment operators are evaluated from left to right; 除赋值运算符外,所有二进制运算符均从左向右求值; assignment operators are evaluated right to left. 赋值运算符从右到左评估。

Crucially, the precedence then left-to-right evaluation will be repeated, most easily illustrated by going through your example step by step: 最重要的是,将重复从左到右的优先级评估,最简单的方法是通过逐步执行示例来说明:

(4 * 4) + (8 * 8) * (4 * 4) - 16/4
// Found brackets - highest precedence.
// more than one, so evaluate left to right
16 + (8 * 8) * (4 * 4) - 16/4
16 + 64 * (4 * 4) - 16/4
16 + 64 * 16 - 16/4
//brackets finished, back to precedence.  Found multiplicative operators
//evaluate left to right
16 + 1024 - 16/4
16 + 1024 - 4
//multiplicative finished, back to precedence.  Found additive operators
//evaluate left to right
1040 - 4
1036
// Expression fully evaluated. Can now be used / assigned to a variable

Addendum 附录

The definitive specification for the evaluation of expressions is in the Java Language Specification chapter 15 . 有关表达式求值的权威性规范,请参见Java语言规范第15章 It's probably more confusing than helpful at this level, but I include it for completeness. 在这个级别上,它可能比帮助有用而更加令人困惑,但是为了完整起见,我将其包括在内。 Section 15.7.3 deals with respecting brackets (parantheses) around expressions and operator precedence. 第15.7.3节处理有关表达式和运算符优先级的方括号(括号)。

Usually, the code is always read from left to right, but also follows the math precedence. 通常,代码始终从左到右读取,但也遵循数学优先级。 Example: 例:

(4 * 4) + (8 * 8) * (4 * 4) - 16/4

Math says in this case: Parentheses first, multiplication and division after, and then addition and subtraction. 数学在这种情况下说:先加括号,再加和除,然后加减。 Then: 然后:

//Parenthesis done
(16) + (64) * (16) - 16/4

//Multiplication and division done
16 + 1024 - 4

//Addition and subtraction done
1036

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM