# 程式範例

> 程式是由許多敘述組成的，而敘述的基本單位是運算元與運算子。 透過本章學習如何進行運算式之資料型態的轉換。

### 一、認識運算式

* 運算式由運算元（operand）與運算子（operator）組成
* 運算元可以是變數或是常數
* 運算子就是數學上的運算符號
* 如「+」、「-」、「\*」、「/」等

```
public class ch04_1 {

	public static void main(String[] args) {
		int apple=19;

		System.out.println("計算之前, apple= "+apple);  //首先印出apple的值
		apple=apple+1;   // 將apple的值加一再設定給apple存放
		System.out.println("計算之後, apple= "+apple); //印出計算後的apple值

	}
```

OUTPUT：\
計算之前, apple= 19\
計算之後, apple= 20

### 二、一元運算子

一元運算子只需要一個運算元\
使用！運算元必須為 boolean 型態

```
public class ch04_2 {

	public static void main(String[] args) {
		boolean a=true;  //宣告布林變數a為真
		byte b=Byte.MIN_VALUE;  //宣告變數b為最小值

		System.out.println("a= "+a+",!a= "+(!a)); //分別印出a與!a 的值
		System.out.println("b= "+b+",~b= "+(~b)); //分別印出b與~b 的值

	}
```

OUTPUT：\
a= true,!a= false\
b= -128,\~b= 127

### 三、關係運算子與 if 敘述

如果括號中的條件判斷成立，就會執行後面的敘述；若是條件判斷不成立時，則後面的敘述就不會執行。

```
public class ch04_3 {

	public static void main(String[] args) {
		if(19>18)
			System.out.println("19>18成立");
		if(true)
			System.out.println("判斷為真，代表印得出字串");
		if(false)
			System.out.println("判斷為假，代表無法印出字串");
		if(19==18)
			System.out.println("如果數值19等於18 才會印出這個字串");
	}
```

OUTPUT：\
19>18成立\
判斷為真，代表印得出字串

### 四、遞增與遞減運算子

> a++ 會先執行整個敘述後再將a的值加 1
>
> ++a則先把a的值加 1 後再執行整個敘述

```
public class ch04_4 {

	public static void main(String[] args) {
		int a=5,b=5,c;

		System.out.println("a= "+a+", b= "+b); //首先印出a與b

		c=a++;   //相當於 c=a;a=a+1;
		System.out.println("c= "+c+", a= "+a); //印出a++

		c=++b;   //相當於 b=b+1;c=b;
		System.out.println("c= "+c+", b= "+b); //印出++b

	}
```

OUTPUT：\
a= 5, b= 5\
c= 5, a= 6\
c= 6, b= 6

### 五、邏輯運算子

{% hint style="info" %}
a>0 && b>0 // 兩個運算元皆為真，運算結果才為真&#x20;

a>0 || b>0 // 兩個運算元只要一個為真，運算結果就為真
{% endhint %}

```
public class ch04_5 {

	public static void main(String[] args) {
		int a=59;

		if((a<0) || (a>100))
			System.out.println("輸入成績錯誤");

		if((a<60) && (a>49))
			System.out.println("需要補考");

	}
```

OUTPUT：\
需要補考

### 六、運算式

運算式可以由數值、變數或是其他運算元與運算子所組合而成。

```
public class ch04_6 {

	public static void main(String[] args) {
		int a=2,b=6;

		System.out.println("計算 a+=b 前, a= "+a+", b= "+b);
		a+=b;
		System.out.println("計算 a+=b 後, a= "+a+", b= "+b);
	}
```

OUTPUT：\
計算 a+=b 前, a= 2, b= 6\
計算 a+=b 後, a= 8, b= 6

### 七、運算式的型態轉換

* 佔用位元組較少的型態轉換成位元組較多的型態
* 字元型態會轉換成short型態（字元會取其unicode碼）
* short型態（2 bytes）遇上int型態（4 bytes），會轉換成int型態
* int型態會轉換成float型態
* 運算式中若某個運算元的型態為double，則另一個運算元也會 轉換成double型態
* 布林型態不能轉換成其它的型態

```
public class ch04_7 {

	public static void main(String[] args) {
		char ch='X';
		short s=-5;
		int i=6;
		float f=9.7f;
		double d=1.76;

		System.out.print("(s*ch)-(d/f)*(i+f)= ");
		System.out.println((s*ch)-(d/f)*(i+f));
	}

```

OUTPUT：\
(s\*ch)-(d/f)\*(i+f)= -442.8486598152212

![運算式的型態轉換圖解](https://2439647256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4NDwp0sFRvD07cdujE%2F-M4giPnxM_2kzTawWa8c%2F-M4gj8lHHpaOOMjrFeQp%2Fimage.png?alt=media\&token=c5404a1f-29b7-4d85-a36b-a1d3a56e15b1)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java.4-x.tw/java-04/java-04-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
