Operator | Description | Expression | Example |
---|---|---|---|
[] | 数组下标 | ArrayName[number] |
arr[0] |
() | 圆括号 | (Expression) |
Function(Parameter) | () |
. | 成员选择(对象) | Object.Memeber |
person.age |
-> | 成员选择(指针) | Object->Member |
person->age |
- | 负号运算符 | -Expression |
-2 |
({Type}) | 强制类型转换 | (Type)Expression |
(Int)8 |
++ | 自增运算符 | ++Variable / Variable++ |
++n(先n自增,再取n的值) / n++(先取n的值,再n自增) |
-- | 自减运算符 | --Variable / Variable-- |
--n(先n自减,再取n的值) / n--(先取n的值,再n自减) |
* | 取值运算符 | *Pointer |
*personPointer |
& | 取地址运算符 | &Variable |
&person |
! | 逻辑非运算符 | !Expression |
!person.isMale |
~ | 取反运算符 | ~Expression |
1 变 0, 0 变 1; ~0100 0011 = 1011 1100 |
sizeof | 长度运算符 | sizeof(Expression) |
sizeof(person) |
/ | 除 | Expression / Expression |
3 / 4 |
* | 乘 | Expression * Expression |
3 * 4 |
% | 余数(取模) | Expression % Expression |
3 % 4 |
+ | 加 | Expression + Expression |
3 + 4 |
- | 减 | Expression - Expression |
3 - 4 |
<< | 左移 | Variable << Expression |
0100 0011 << 2 = 0000 1100 |
>> | 右移 | Variable >> Expression |
0100 0011 >> 1 = 0010 0001 |
> | 大于 | Expression > Expression |
4 > 3 |
>= | 大于等于 | Expression >= Expression |
4 >= 3 |
< | 小于 | Expression < Expression |
3 < 4 |
<= | 小于等于 | Expression <= Expression |
3 <= 4 |
== | 等于 | Expression == Expression |
3 == 3 |
!= | 不等于 | Expression != Expression |
3 != 4 |
& | 按位与 | Expression & Expression |
x & 0 = 0, 1 & 1 = 1; 0000 0011 & 0000 0010 = 0000 0010 |
^ | 按位亦或 | Expression ^ Expression |
同 0 异 1; 0000 0011 & 0000 0010 = 0000 0001 |
| | 按位或 | Expression | Expression |
x | 1 = 1, 0 | 0 = 0; 0000 0011 | 0000 0010 = 0000 0011 |
&& | 逻辑与 | Expression && Expression |
true && false, && 和 & 的区别: && 会提前短路 |
|| | 逻辑或 | Expression || Expression |
true || false |
?: | 条件运算符 | Expression ? Expression : Expression |
3 > 4 ? true : false |
= | 赋值运算符 | Variable = Expression |
person.age = 18 |
/= | 除后赋值 | Variable /= Expression |
person.age /= 18 |
= | 乘后赋值 | Variable *= Expression |
person.age *= 18 |
%= | 取模后赋值 | Variable %= Expression |
person.age %= 18 |
+= | 加后赋值 | Variable += Expression |
person.age += 18 |
-= | 减后赋值 | Variable -= Expression |
person.age -= 18 |
<<= | 左移后赋值 | Variable <<= Expression |
x <<= 3 |
>>= | 右移后赋值 | Variable >>= Expression |
x >>= 3 |
&= | 按位与后赋值 | Variable &= Expression |
x &= 3 |
^= | 按位亦或后赋值 | Variable ^= Expression |
x ^= 3 |
|= | 按位或后赋值 | Variable |= Expression |
x |= 3 |
, | 逗号运算符 | Expression, Expression, Expression |
x = 3, y = 4, z = 5 |
网友评论