跳轉到內容

算術表示式

100% developed
來自華夏公益教科書,開放的書籍,開放的世界

導航 語言基礎 主題:v  d  e )

為了在 Java 中進行算術運算,必須首先宣告至少一個變數。通常,在進行任何算術運算之前,會宣告一個變數併為其賦值。以下是一個宣告整數變數的示例

Example 程式碼部分 3.59:變數賦值。
int x = 5;

建立變數後,可以使用 Java 的運算子來操作其值:+(加法)、-(減法)、*(乘法)、/(整數除法)、% (取模或餘數)、++(字首和字尾自增一)、--(字首和字尾自減一)。

Computer code 程式碼清單 3.10:Operators.java
public class Operators {
  public static void main(String[] args) {
    int x = 5;
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Addition             ---");
    x = 5;
    System.out.println("x + 2 = " + (x + 2));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Subtraction          ---");
    x = 5;
    System.out.println("x - 4 = " + (x - 4));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Multiplication       ---");
    x = 5;
    System.out.println("x * 3 = " + (x * 3));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- (Integer) Division   ---");
    x = 5;
    System.out.println("x / 2 = " + (x / 2));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Modulo (Remainder)   ---");
    x = 5;
    System.out.println("x % 2 = " + (x % 2));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Preincrement by one  ---");
    x = 5;
    System.out.println("++x   = " + (++x  ));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Predecrement by one  ---");
    x = 5;
    System.out.println("--x   = " + (--x  ));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Postincrement by one ---");
    x = 5;
    System.out.println("x++   = " + (x++  ));
    System.out.println("x = " + x);
    System.out.println();
   
    System.out.println("--- Postdecrement by one ---");
    x = 5;
    System.out.println("x--   = " + (x--  ));
    System.out.println("x = " + x);
    System.out.println();
  }
}
Standard input or output 程式碼清單 3.10 的控制檯
x = 5

--- Addition             ---
x + 2 = 7
x = 5

--- Subtraction          ---
x - 4 = 1
x = 5

--- Multiplication       ---
x * 3 = 15
x = 5

--- (Integer) Division   ---
x / 2 = 2
x = 5

--- Modulo (Remainder)   ---
x % 2 = 1
x = 5

--- Preincrement by one  ---
++x   = 6
x = 6

--- Predecrement by one  ---
--x   = 4
x = 4

--- Postincrement by one ---
x++   = 5
x = 6

--- Postdecrement by one ---
x--   = 5
x = 4

除法運算子向零取整:5/2 為 2,-5/2 為 -2。餘數運算子與左運算元符號相同;它定義為 ((a/b)*b) + (a%b) 始終等於 a。字首自增、字首自減、字尾自增和字尾自減運算子是特殊的:它們還會透過加或減一來更改變數的值。唯一的區別是字首自增/自減返回變數的新值;字尾自增返回變數的原始值。

測試你的知識

問題 3.8:考慮以下程式碼

Computer code 問題 3.8:Question8.java
public class Question8 {
  public static void main(String[] args) {
    int x = 10;
    x = x + 10;
    x = 2 * x;
    x = x - 19;
    x = x / 3;
    System.out.println(x);
  }
}

標準輸出中會列印什麼?

答案
Standard input or output 問題 3.8 的輸出
7

int x = 10; => 10
x = x + 10; => 20
x = 2 * x; => 40
x = x - 19; => 21
x = x / 3; => 7

當在一個表示式中使用多個運算子時,必須考慮 Java 的運算子優先順序。Java 使用標準 PEMDAS(括號、指數、乘法和除法、加法和減法)順序。當存在多個相同優先順序的運算子時,Java 會從左到右進行計算。考慮以下程式碼的輸出是什麼

Example 程式碼部分 3.60:多個運算子。
System.out.println(10*5 + 100/10 - 5 + 7%2);
Standard input or output 程式碼部分 3.60 的控制檯
56

下表顯示了 Java 如何計算此表示式


圖 3.1:Java 程式語言中算術表示式的計算


除了執行數學函式之外,還有一些運算子用於將數字賦值給變數(每個示例都使用初始化為 x = 5 的變數)

Computer code 程式碼清單 3.11:Assignments.java
public class Assignments {
  public static void main(String[] args) {
    int x = 5;
    x = 3;
    System.out.println("Assignment                                       (x = 3) : " + x);

    x = 5;
    x += 5;
    System.out.println("Assign x plus another integer to itself          (x += 5): " + x);

    x = 5;
    x -= 4;
    System.out.println("Assign x minus another integer to itself         (x -= 4): " + x);

    x = 5;
    x *= 6;
    System.out.println("Assign x multiplied by another integer to itself (x *= 6): " + x);

    x = 5;
    x /= 5;
    System.out.println("Assign x divided by another integer to itself    (x /= 5): " + x);
  }
}
Standard input or output 程式碼清單 3.11 的控制檯
Assignment                                       (x = 3) : 3
Assign x plus another integer to itself          (x += 5): 10
Assign x minus another integer to itself         (x -= 4): 1
Assign x multiplied by another integer to itself (x *= 6): 30
Assign x divided by another integer to itself    (x /= 5): 1

在 Java 中使用位運算子

[編輯 | 編輯原始碼]

除了算術運算子外,Java 還有一組位運算子來運算元字中的位,以及一組邏輯運算子。位邏輯運算子有

運算子 函式 的值
x 之前
例子
輸入
例子
輸出
的值
x 之後
& 按位與 7 x&27 3 7
| 按位或 7 x|27 31 7
^ 按位異或 7 x^27 28 7
~ 按位取反 7 ~x -8 7

除了這些邏輯位運算子之外,還有一些運算子用於將數字賦值給變數(x = -5

運算子 函式 例子
輸入
示例輸出
&= 將與另一個值按位與的 x 賦值給自己 x &= 3 3
|= 將與另一個值按位或的 x 賦值給自己 x |= 3 -5
^= 將與另一個值按位異或的 x 賦值給自己 x ^= 3 -8
<<= x 除以另一個整數並賦值給自己 x <<= 1 -10
>>= x 按位取反並賦值給自己 x >>= 1 -3
>>>= x 按位取反並賦值給自己 x >>>= 1 2,305,843,009,213,693,949 (64 位)

移位運算子用於將位左移或右移,這也是快速將數字乘以或除以 2 的方法

運算子 函式 的值
x 之前
例子
輸入
示例輸出 的值
x 之後
<< 邏輯左移 -15 x << 2 -60 -15
>> 算術右移 -15 x >> 3 -2 -15
>>> 邏輯右移 -15 x >>> 3 2,305,843,009,213,693,937 (64 位) -15


華夏公益教科書