时间:2020-10-15 23:16:59 | 栏目:.NET代码 | 点击:次
常见的操作符
赋值操作符:=
复合赋值运算符:+=,-=,*=,/=,%=
算数运算符:+,-,*,/,%(ß二元运算符),++,--(ß一元运算符)
条件运算符:>,<,>=,<=,!=,==
逻辑运算符:&&,||,|,!
几乎所有的操作符都只能操作基本数据类型,但是”=”,”==”和”!=”,这些操作符能操作所有的对象。除此以外String类支持”+”和”+=”。
优先级
当一个表达式中存在多个操作符时,操作符的优先级便决定了表达式中各部分的运算顺序。Java中操作符有自己的一套计算顺序,牢记顺序可避免程序出现运算错误。最简单的就是先乘除后加减
操作符优先级
postfix operators |
[] . (params) expr++ expr-- |
unary operators |
++expr --expr +expr -expr ~ ! |
creation or cast |
new (type)expr |
multiplicative |
* / % |
additive |
+ - |
shift |
<< >> >>> |
relational |
< > <= >= instanceof |
equality |
== != |
bitwise AND |
& |
bitwise exclusive OR |
^ |
bitwise inclusive OR |
| |
logical AND |
&& |
logical OR |
|| |
conditional |
? : |
assignment |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
Operator |
Use |
Equivalent to |
+= |
op1 += op2 |
op1 = op1 + op2 |
-= |
op1 -= op2 |
op1 = op1 - op2 |
*= |
op1 *= op2 |
op1 = op1 * op2 |
/= |
op1 /= op2 |
op1 = op1 / op2 |
%= |
op1 %= op2 |
op1 = op1 % op2 |
&= |
op1 &= op2 |
op1 = op1 & op2 |
|= |
op1 |= op2 |
op1 = op1 | op2 |
^= |
op1 ^= op2 |
op1 = op1 ^ op2 |
<<= |
op1 <<= op2 |
op1 = op1 << op2 |
>>= |
op1 >>= op2 |
op1 = op1 >> op2 |
>>>= |
op1 >>>= op2 |
op1 = op1 >>> op2 |