1. C++ operator precedence
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 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 |
=
| 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 | ||
| 16 | throw | Throw operator (for exceptions) | |
| 17 | , | Comma | Left-to-right |
2. Java operator precedence
| Priority | Operators | Operation | Associativity |
|---|---|---|---|
| 1 | [ ] | array index | left |
() | method call | ||
. | member access | ||
| 2 | ++ | pre- or postfix increment | right |
-- | pre- or postfix decrement | ||
+ - | unary plus, minus | ||
~ | bitwise NOT | ||
! | boolean (logical) NOT | ||
(type) | type cast | ||
new | object creation | ||
| 3 | * / % | multiplication, division, remainder | left |
| 4 | + - | addition, substraction | left |
+ | string concatenation | ||
| 5 | << | signed bit shift left | left |
>> | signed bit shift right | ||
>>> | unsigned bit shift right | ||
| 6 | < <= | less than, less than or equal to | left |
> >= | greater than, greater than or equal to | ||
instanceof | reference test | ||
| 7 | == | equal to | left |
!= | not equal to | ||
| 8 | & | bitwise AND | left |
& | boolean (logical) AND | ||
| 9 | ^ | bitwise XOR | left |
^ | boolean (logical) XOR | ||
| 10 | | | bitwise OR | left |
| | boolean (logical) OR | ||
| 11 | && | boolean (logical) AND | left |
| 12 | || | boolean (logical) OR | left |
| 13 | ? : | conditional | right |
| 14 | = | assignment | right |
*= /= += -= %= | combinated assignment (operation and assignment) | ||
reference:
1. http://en.cppreference.com/w/cpp/language/operator_precedence
2.http://bmanolov.free.fr/javaoperators.php
本文详细介绍了C++和Java中运算符的优先级及结合性,包括算术运算符、比较运算符、逻辑运算符等,并对比了两种语言间的差异,有助于开发者更好地理解和使用这两种流行的编程语言。

被折叠的 条评论
为什么被折叠?



