新手瞭解java基礎知識(二)

算數運算符

算術運算符用在數學表達式中,它們的作用和在數學中的作用一樣。下表列出瞭所有的算術運算符。

表格中的實例假設整數變量A的值為10,變量B的值為20:

操作符 描述
+ 加法 – 相加運算符兩側的值
減法 – 左操作數減去右操作數
* 乘法 – 相乘操作符兩側的值
/ 除法 – 左操作數除以右操作數
% 取餘 – 左操作數除以右操作數的餘數
++ 自增: 操作數的值增加1
– – 自減: 操作數的值減少1

例子

public static void main(String[] args) {
     int a = 10;
     int b = 20;
     int c = 25;
     int d = 25;
     System.out.println("a + b = " + (a + b) );
     System.out.println("a - b = " + (a - b) );
     System.out.println("a * b = " + (a * b) );
     System.out.println("b / a = " + (b / a) );
     System.out.println("b % a = " + (b % a) );
     System.out.println("c % a = " + (c % a) );
     System.out.println("a++   = " +  (a++) );
     System.out.println("a--   = " +  (a--) );
     // 查看  d++ 與 ++d 的不同
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
  運行結果
  "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=51196:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\rt.jar;D:\JavaProject\20210402\out\production\20210402" Test
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
a--   = 11
d++   = 25
++d   = 27
Process finished with exit code 0

關系運算符

運算符 描述
== 檢查如果兩個操作數的值是否相等,如果相等則條件為真。
!= 檢查如果兩個操作數的值是否相等,如果值不相等則條件為真。
> 檢查左操作數的值是否大於右操作數的值,如果是那麼條件為真。
< 檢查左操作數的值是否小於右操作數的值,如果是那麼條件為真
>= 檢查左操作數的值是否大於或等於右操作數的值,如果是那麼條件為真。
<= 檢查左操作數的值是否小於或等於右操作數的值,如果是那麼條件為真。

例子

public static void main(String[] args) {
     int a = 10;
     int b = 20;
     System.out.println("a == b = " + (a == b) );
     System.out.println("a != b = " + (a != b) );
     System.out.println("a > b = " + (a > b) );
     System.out.println("a < b = " + (a < b) );
     System.out.println("b >= a = " + (b >= a) );
     System.out.println("b <= a = " + (b <= a) );
  }
  運行結果
"C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=51452:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\rt.jar;D:\JavaProject\20210402\out\production\20210402" Test
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
Process finished with exit code 0

邏輯運算符

操作符 描述
&& 稱為邏輯與運算符。當且僅當兩個操作數都為真,條件才為真。
ll 稱為邏輯或操作符。如果任何兩個操作數任何一個為真,條件為真。
稱為邏輯非運算符。用來反轉操作數的邏輯狀態。如果條件為true,則邏輯非運算符將得到false

例子

 public static void main(String[] args) {
     boolean a = true;
     boolean b = false;
     System.out.println("a && b = " + (a&&b));
     System.out.println("a || b = " + (a||b) );
     System.out.println("!(a && b) = " + !(a && b));
  }

在這裡插入圖片描述

賦值運算符

賦值運算符 描述 例子
= 簡單的賦值運算符,將右操作數的值賦給左側操作數 C = A + B將把A + B得到的值賦給C
+ = 加和賦值操作符,它把左操作數和右操作數相加賦值給左操作數 C + = A等價於C = C + A
– = 減和賦值操作符,它把左操作數和右操作數相減賦值給左操作數 C – = A等價於C = C – A
* = 乘和賦值操作符,它把左操作數和右操作數相乘賦值給左操作數 C * = A等價於C = C * A
/ = 除和賦值操作符,它把左操作數和右操作數相除賦值給左操作數 C / = A,C 與 A 同類型時等價於 C = C / A
(%)= 取模和賦值操作符,它把左操作數和右操作數取模後賦值給左操作數 C%= A等價於C = C%A
<< = 左移位賦值運算符 C << = 2等價於C = C << 2
>> = 右移位賦值運算符 C >> = 2等價於C = C >> 2
&= 按位與賦值運算符 C&= 2等價於C = C&2
^ = 按位異或賦值操作符 C ^ = 2等價於C = C ^ 2
l= 按位或賦值操作符 C l = 2等價於C = C
  public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 0;
        c = a + b;
        System.out.println("c = a + b = " + c );
        c += a ;
        System.out.println("c += a  = " + c );
        c -= a ;
        System.out.println("c -= a = " + c );
        c *= a ;
        System.out.println("c *= a = " + c );
        a = 10;
        c = 15;
        c /= a ;
        System.out.println("c /= a = " + c );
        a = 10;
        c = 15;
        c %= a ;
        System.out.println("c %= a  = " + c );
        c <<= 2 ;
        System.out.println("c <<= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c &= a ;
        System.out.println("c &= a  = " + c );
        c ^= a ;
        System.out.println("c ^= a   = " + c );
        c |= a ;
        System.out.println("c |= a   = " + c );
    }
運行結果為:
c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a  = 0
c ^= a   = 10
c |= a   = 10
Process finished with exit code 0

Java運算符優先級

類別 操作 關聯性
後綴 () [] . (點操作符) 左到右
一元 expr++ expr– 從左到右
一元 ++expr –expr + – ~ ! 從右到左
乘性 * /% 左到右
加性 + – 左到右
移位 >> >>> << 左到右
關系 > >= < <= 左到右
相等 == != 左到右
按位與 左到右
按位異或 ^ 左到右
按位或 l 左到右
邏輯與 && 左到右
邏輯或 ll 左到右
條件 ?: 從右到左
賦值 = + = – = * = / =%= >> = << =&= ^ = l= 從右到左
逗號 左到右

總結

本篇文章就到這裡瞭,希望對你有所幫助,也希望你能多多關註WalkonNet的更多內容!

推薦閱讀: