0

## 前言

1. 算術運算子(Arithmetic Operators)
2. 賦值運算子(Assignment Operators)
3. 關係運算子(Relational Operators)
4. *三目運算子(Ternary Operators)
5. 邏輯運算子(Logical Operators)

## 算數運算子

``````int num1 = 10;
int num2 = 20
int num3 = 0;

num3 = 12 + num2;
num3 = num1 * 2;
num3 = num1 / 2;
num3 = num2 % 2;
``````

``````int num1 = 5;
int num2 = 2;
int num3 = 0;

num3 = num1 + num2 * 2; // 9
num3 = (num1 + num2) * 2; // 14
``````

``````int num = 17;
int op1 = 3;
float op2 = 3.0f;

System.out.println("test 1 = " +num/op1); // 5
System.out.println("test 2 = " +num/op2); // 5.6666665
``````

``````float num1 = 17.0f;
float num2 = 17.9f;
float op1 = 3.0f;

System.out.println("test 1 = " +num1%op1); // 2.0
System.out.println("test 2 = " +num2%op1); // 2.8999996
``````

### 單目運算子

``````/* 正負號表示 */
int num1 = -10;
int num2 = +15;

/* 自增與自減運算 */
System.out.println(num1++); // -10
System.out.println(--num2); // 14
``````

``````int num = 10;
int result = 0;

result = ++num;

System.out.println("result = " + result);
System.out.println("num = " + num);
``````

``````result = 11
num = 11
``````

``````int num = 10;
int result = 0;

result = num--;

System.out.println("result = " + result);
System.out.println("num = " + num);
``````

``````result = 10
num = 9
``````

``````int a = 4;

a = (++a) + 4; // a = 5 + 4
System.out.println("a = "+a);

a = (a++) + 4; // a = 9 + 4
System.out.println("a = "+a);

a += (++a); // a = 13 + 14
System.out.println("a = "+a);

a -= (a--); // a = 27 - 27
System.out.println("a = "+a);
``````

``````a = 9
a = 13
a = 27
a = 0
``````

``````int num = 10;
int i = 5;

/* 盡量避免使用這種風格的寫法 */
num += i++;
num += ++i;
num = (num++) * (num++);
num = (++num) + (num++);
``````

## 指定運算子

``````int num1;
char num2;
String num3;

num1 = 15;
num2 = 'a';
num3 = "string...";
``````

``````100.0d = d;
num1 == 0;
num1 + num2 = 5;
``````

### 複合指定運算子

1. +=
2. -=
3. *=
4. /=
5. %=

``````int num1 = 10;
float num2 = 122.6.f;
double num3 = 1.0;

num3 += num1; // 1.0 + 10
num3 = 1.0;

num3 -= num1; // 1.0 - 10
num3 = 1.0;

num3 *= num2; // 1.0 * 122.6
num3 = 1.0;

num3 /= num2; // 1.0 / 122.6
num3 = 1.0;

num3 %= num1; // 1.0 % 10
num3 = 1.0;

``````

### 雙目運算子的賦值方向

``````int num1 = 3;
int num2 = (num1=4) * num1; // num1已經被改為4，所以執行運算為4 * 4

System.out.println(num2);
``````

``````num2 = 16
``````

``````int num = 9;
num += (num=10); // 9 + 10
System.out.println("num = " + num);

num = (num=3) + num; // 3 + 3
System.out.println("num = " + num);

num -= (num=1); // 6 - 1
System.out.println("num = " + num);
``````

``````num = 19
num = 6
num = 5
``````

``````int num = 10;
num += num -= num *= num /= num;

/* 相當於 */
num = num + (num - (num * (num / num)));
``````

``````num = 10
``````

## 關係運算子

``````int num = 10;

if(num < 100)
System.out.println(num < 100);
else
System.out.println(num < 100);
``````

``````true
``````

java提供多組關係運算子給條件語句進行判斷

1. >
2. <
3. >=
4. <=
5. ==
6. !=

``````int num1 = 10;
float num2 = 10.0f;

if(num1 == num2){
System.out.println(num1 == num2);
}
else{
System.out.println(num1 == num2);
}
``````

``````true
``````

``````char ch = 'a'; // a ASCII碼為97

if(ch >= 100)
System.out.println(ch > 100);
else
System.out.println(ch > 100);

``````

``````false
``````

``````System.out.println(10 <= 100);

System.out.println(true == false);
System.out.println("123" != "124");
``````

``````true
false
true
``````

## 三目運算子

`條件判斷 ? 成立返回值 : 失敗返回值`

``````int num1=6, num2=9;
boolean b = num1 > num2 ? (13 > 6) : (true == false);

System.out.println(b);
``````

``````false
``````

``````int num1=6, num2=9;
boolean b=false;

if(num1 > num2)
b = (13 > 6);
else
b = (true == false);

System.out.println(b);

``````

``````int num1 = 18, num2 = 44, num3 = 90;
int max;

max = ((num1 > num2) ? (num1 > num3) ? num1 : num3 : (num2 > num3) ? num2 : num3);

System.out.println("最大值是: " + max);
``````

``````最大值是: 90
``````

## 邏輯運算子

java主要提供三種邏輯運算子，分別是AND, OR, NOT運算:

1. &
2. |
3. !
``````int num1 = 10;
int num2 = 101;

if(num1 > 5 & num2 > 100)
System.out.println(num1 >5 & num2 > 100); // true AND ture = true

if(num1 > 10 | num2 > 100) // false OR true = true
System.out.println(num1 > 10 | num2 > 100);

if(!(num1 > 10) && !(num2 < 100)) // NOT(false) AND NOT(false) = true
System.out.println(!(num1 > 10) && !(num2 < 100));

``````

``````true
true
true
``````

### 使用短路運算子提高效率

``````int num = 0;

if((4 > 5) && (7 > 3)){ // A
num++;
}

if((4 < 5) || (5 < 6)){ // B
num++;
}
``````

``````int i = 5;

if((2*i > 5) | (++i > 2))
System.out.println("i = " + i);

i = 5;

if((2*i > 5) || (++i > 2))
System.out.println("i = " + i);
``````

``````i = 6
i = 5
``````

## 運算子優先級

``````double x=1,y=2,z=3;
double d = 2 * x * y + z - 1 / x + z % 2; // 4 + 3 - 1 + 1

System.out.println("d = " + d);
``````

``````d = 7.0
``````

++, -- 右到左 算術運算子
+(正號), -(負號), ! 右到左 算術運算子
*, % 左到右 算術運算子
+, - 左到右 算術運算子
>, <, >=, <= 左到右 關係運算子
==, != 左到右 關係運算子
& 左到右 邏輯運算子
| 左到右 邏輯運算子
&& 左到右 邏輯運算子
|| 左到右 邏輯運算子
?: 右到左 三目運算子(條件運算子)
=, +=, -=, *=, /=, %= 右到左 複合指定運算子

### 運算運算與優先級問題

``````int num = 5;

System.out.println(num++ * ++num * num++);
``````

``````245
``````

`num++ -> num++ -> ++num`

``````int num1 = 5;
int num2 = 6;
int num3 = 1;
int result = 0;

result = num1 * ++num2 * num3++;
result = ((num1 * (++num2)) * (num3++)); // 解釋成這樣
``````

``````int num = 5;

System.out.println("test1 = " + (num++ * ++num));

num = 5;

System.out.println("test2 = " + (++num * num++));
``````

``````test1 = 35
test2 = 36
``````

test1的運算順序:

1. `num++`得到返回值5
2. `++num`得到返回值7
3. 兩個操作變數相乘得到35

test2的運算順序

1. `++num`得到返回值6
2. `num++`得到返回值6
3. 兩個操作變數相乘得到36

1. `num++`得到返回值5
2. `++num`得到返回值7
3. 兩個操作變數相乘得到35
4. `num++`得到返回值7
5. 兩個操作變數相乘得到245