Learn Java - Chapter 2 操作符(Operator)

來源:互聯網
上載者:User

標籤:

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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.