標籤:
1、賦值操作符
= 賦值
eg:
int cadence = 0;int speed = 0;int gear = 1;
2、基本數學運算子
+ 加 (兩邊是數值型變數或值作數學運算,其中一個為字元型變數或值作串連運算)
- 減
* 乘
/ 除 (兩個整數作除法,結尾取整,原文:Integer division rounds toward 0)
% 取餘
3、一元運算子
+ 表示正數
- 表示負數
++ 自增運算 每次加1。在變數之前表示先+1,再使用該變數;在變數之後表示使用該變數後再+1
-- 自減運算 每次減1。在變數之前表示先-1,再使用該變數;在變數之後表示使用該變數後再-1
!邏輯運算子,取反
int i = 3;i++;// prints 4System.out.println(i);++i; // prints 5System.out.println(i);// prints 6System.out.println(++i);// prints 6System.out.println(i++);// prints 7System.out.println(i);
4、關係運算子
== 判斷相等(基本類型根據值作對比,判斷是否相等;參考型別根據對象引用的地址判斷是否相等)!= 判斷不等於(同上)> greater than>= greater than or equal to< less than<= less than or equal to
以上關係運算子通常用作基本類型的比較,對象的比較通常使用對象的equals方法。
5、條件運算子
&& 且
|| 或
int value1 = 1;int value2 = 2;if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2");if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1");
6、類型檢測
instanceof
用於判斷一個執行個體是否是某個類(某個類或某個類的子類或某個介面)的執行個體。
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); }} class Parent {}class Child extends Parent implements MyInterface {}interface MyInterface {}
輸出:
obj1 instanceof Parent: trueobj1 instanceof Child: falseobj1 instanceof MyInterface: falseobj2 instanceof Parent: trueobj2 instanceof Child: trueobj2 instanceof MyInterface: true
7、位移運算子
<< 左移運算>> 右移運算>>> 無符號右移運算& 按位與^ 按位異或| 按位或~ 按位取反(非)
Learn Java - Chapter 2 操作符(Operator)