分类: Java
2008-08-03 23:55:45
Operators |
Precedence |
后置操作符 |
expr++ expr-- |
一元操作符 |
++expr --expr +expr -expr ~ ! |
乘法运算 |
* / % |
加法运算 |
+ - |
移位 |
<< >> >>> |
关系操作符 |
< > <= >= instanceof |
等于 |
== != |
按位与 |
& |
按位异或 |
^ |
按位或 |
| |
逻辑与 |
&& |
逻辑或 |
|| |
三元操作符 |
? : |
赋值操作 |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
public class Operator {
public static void main(String[] args){
byte left = 4; //00000100
byte right = 8;//00001000
//后置操作符
left--;
System.out.println(left);
left++;
System.out.println(left);
//一元操作符
System.out.println(++left);
System.out.println(--left);
System.out.println(+left);
System.out.println(-left);
System.out.println(~left);
System.out.println(!true);
//乘法操作符
System.out.println(left * right);
System.out.println(right / left);
System.out.println(right % left);
//加法操作符
System.out.println(left + right);
System.out.println(left - right);
//移位操作符
System.out.println(left >> 1);
System.out.println(left << 1);
System.out.println(left >>> 1);
//关系操作符
System.out.println(left > right);
System.out.println(left < right);
System.out.println(left == right);
Operator op = new Operator();
System.out.println(op instanceof Operator);
//位操作符
System.out.println(left & right);//00000100 & 00001000 = 00000000
System.out.println(left | right);//00000100 | 00001000 = 00001100
System.out.println(left ^ right);//00000100 ^ 00001000 = 00001100
//逻辑操作符
System.out.println(false && true);
System.out.println(false || true);
//三元操作符
System.out.println((int)left > (int)right ? left : right);
//赋值操作符
System.out.println(left += right);
System.out.println(left -= right);
System.out.println(right *= left);
System.out.println(right /= left);
System.out.println(left <<= 1);
System.out.println(left >>= 1);
}
}
1.2.1. 后置操作符
++/--语句执行完毕后再进行操作
System.out.println(left++);//4
System.out.println(left--);//5
1.2.2. 前置操作符
先进行操作符运算再进行语句操作
System.out.println(++left);//5
System.out.println(--left);//4
+:整数,可以省略
-:求负
~:求反
1.2.3. 乘法操作符
*:乘法
/:除法
%:求余数
1.2.4. 加法操作符
+:和运算
-:减运算
1.2.5. 移位操作符
>>:向右移位,移动一位相当于除以2;
<<:向左移位,移动一位相当于乘以2;
>>>:代符号移位,保留数值的正负;
1.2.6. 关系操作符
>:大于
<:小于
==:等于
instanceof:是否为某一类型
1.2.7. 位操作符
&:按位与
|:按位或
^:异或
1.2.8. 逻辑操作符
&&:逻辑与
||:逻辑或
1.2.9. 三元操作符
?::相当于if…else…
(int)left > (int)right ? left : right
if ((int)left > (int)right)
left;
else
right;
1.2.10. 赋值操作符
=:赋值
+=:先进行加运算,再对其赋值;
-=:先进行减运算,再对其赋值;
*=:先进性乘运算,再对其赋值;
/=:先进行除运算,在对其赋值;
|