运算符优先级

2024-10-22

Java 运算符优先级

运算符 优先级
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

C++运算符优先级

优先级 运算符 描述 关联性
1 :: Scope resolution Left-to-right
2 ++   -- Suffix/postfix increment and decrement
() Function call
[] Array subscripting
. Element selection by reference
-> Element selection through pointer
3 ++   -- Prefix increment and decrement Right-to-left
+   - Unary plus and minus
!   ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
new, new[] Dynamic memory allocation
delete, delete[] Dynamic memory deallocation
4 .*   ->* Pointer to member Left-to-right
5 *   /   % Multiplication, division, and remainder
6 +   - Addition and subtraction
7 <<   >> Bitwise left shift and right shift
8 <   <= For relational operators < and ≤ respectively
>   >= For relational operators > and ≥ respectively
9 ==   != For relational = and ≠ respectively
10 & Bitwise AND
11 ^ Bitwise XOR (exclusive or)
12 | Bitwise OR (inclusive or)
13 && Logical AND
14 || Logical OR
15 ?: Ternary conditional Right-to-Left
16 = Direct assignment (provided by default for C++ classes)
+=   -= Assignment by sum and difference
*=   /=   %= Assignment by product, quotient, and remainder
<<=   >>= Assignment by bitwise left shift and right shift
&=   ^=   |= Assignment by bitwise AND, XOR, and OR
17 throw Throw operator (for exceptions)
18 , Comma Left-to-right

C语言运算符优先级

优先级 运算符 描述 关联性
1 ++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2 ++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
3 * / % Multiplication, division, and remainder Left-to-right
4 + - Addition and subtraction
5 << >> Bitwise left shift and right shift
6 < <= For relational operators < and ≤ respectively
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-Left
14 = Simple assignment
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right

PHP运算符优先级

结合方向 运算符 附加信息
非结合 clone new clone 和 new
[ array()
非结合 ++ -- 递增/递减运算符
非结合 ~ - (int) (float) (string) (array) (object) (bool) @ 类型
非结合 instanceof 类型
右结合 ! 逻辑操作符
* / % 算术运算符
+ - . 算术运算符 和 字符串运算符
<< >> 位运算符
非结合 < <= > >= <> 比较运算符
非结合 == != === !== 比较运算符
& 位运算符 和 引用
^ 位运算符
| 位运算符
&& 逻辑运算符
|| 逻辑运算符
? : 三元运算符
= += -= *= /= .= %= &= |= ^= <<= >>= 赋值运算符
and 逻辑运算符
xor 逻辑运算符
or 逻辑运算符
, 多处用到
  • 这个表给出Python的运算符优先级(从低到高).
  • 从最低的优先级(最松散地结合)到最高的优先级(最紧密地结合)。
  • 这意味着在一个表达式中,Python会首先计算表中较下面的运算符,然后在计算列在表上部的运算符。

Python 运算符优先级

运算符 描述
lambda Lambda表达式
or 布尔“或”
and 布尔“与”
not x 布尔“非”
in,not in 成员测试
is,is not 同一性测试
<,<=,>,>=,!=,== 比较
| 按位或
^ 按位异或
& 按位与
<<,>> 移位
+,- 加法与减法
*,/,% 乘法、除法与取余
+x,-x 正负号
~x 按位翻转
** 指数
x.attribute 属性参考
x[index] 下标
x[index:index] 寻址段
f(arguments...) 函数调用
(experession,...) 绑定或元组显示
[expression,...] 列表显示
{key:datum,...} 字典显示
'expression,...' 字符串转换

JS运算符优先级

优先级 运算符 说明 结合性
1 []、.、() 字段访问、数组索引、函数调用和表达式分组 从左向右
2 ++ -- -~!delete new typeof void 一元运算符、返回数据类型、对象创建、未定
义的值
从右向左
3 *、/、% 相乘、相除、求余数 从左向右
4 +、- 相加、相减、字符串串联 从左向右
5 <<、>>、>>> 左位移、右位移、无符号右移 从左向右
6 <、<=、>、>=、instanceof 小于、小于或等于、大于、大于或等于、是否
为特定类的实例
从左向右
7 ==、!=、===、!== 相等、不相等、全等,不全等 从左向右
8 & 按位“与” 从左向右
9 ^ 按位“异或” 从左向右
10 | 按位“或” 从左向右
11 && 短路与(逻辑“与”) 从左向右
12 || 短路或(逻辑“或”) 从左向右
13 ?: 条件运算符 从右向左
14 =、+=、-=、*=、/=、%=、&=、|=、^=、<、<=、>、>=、>>= 混合赋值运算符 从右向左
15 , 多个计算 按优先级计算,然后从右向左

工具介绍

运算符优先级是一种用于帮助程序员了解和查询不同编程语言中的运算符优先级规则在线工具。本工具列出了各种运算符的优先级顺序,包括:Java、Python、C++、C语言、PHP及Javascript,以便开发者在编写代码时参考。

什么是运算符优先级

运算符优先级是编程语言中规定运算符执行顺序的规则。在进行数学运算时,不同的运算符有不同的优先级,例如加法、减法、乘法和除法。运算符优先级对于确保程序正确执行和优化代码性能具有重要意义。

在编程语言中,常见的运算符优先级规则如下:

  • 括号(最高优先级):括号内的表达式优先计算。例如,(a + b) * c。
  • 乘法和除法:乘法(*)和除法(/)运算符具有比加法和减法更高的优先级。例如,a * b + c * d,先进行乘法运算。
  • 加法和减法:加法(+)和减法(-)运算符的优先级较低。例如,a + b - c,先进行加法运算。
  • 按位运算:按位与(&)、按位或(|)、按位异或(^)和按位取反(~)等运算符的优先级低于加减乘除。例如,a & b | c ^ d,按位运算优先。
  • 关系运算:小于(<)、大于(>)、小于等于(<=)、大于等于(>=)、等于(==)和不等于(!=)等关系运算符的优先级低于按位运算。例如,a < b && c > d,先进行关系运算。
  • 逻辑运算:逻辑与(&&)、逻辑或(||)和逻辑非(!)等运算符的优先级低于关系运算。例如,a && b || c,先进行逻辑运算。
  • 赋值运算:赋值运算符(=、+=、-=、*=、/=)的优先级最低。例如,a = b + c,先进行加法运算,然后赋值给a。

在编程时,了解运算符优先级可以帮助我们编写出正确且高效的代码。如果需要改变默认的运算顺序,可以使用括号来强制指定运算顺序。

相关工具