# 更多資料型態

> 此篇介紹字元型態、浮點數與倍精度浮點數型態、布林型態。

### 一、字元型態

在電腦的世界裡，所有的文字、數值都只是一連串的 0 與 1 所組成。\
Java 所使用的 Unicode 標準萬國碼，就是一種可以解決不同語系之間，資訊交流時所發生問題的一種技術。

舉例來說，Unicode 中的大寫 ‘G’ 編碼為 71。\
我們利用不同的方法印出字元’G’：

```
package ch03;

public class ch03_4 {

	public static void main(String[] args) {
		char ch1=71;  //設定字元變數ch1 等於編碼71的字元
		char ch2='G'; //設定字元變數ch2 等於'G'
		char ch3='\u0047'; //以16進位值設定字元變數ch3

		System.out.println("ch1= "+ch1);
		System.out.println("ch2= "+ch2);
		System.out.println("ch3= "+ch3);

	}

}

```

### 二、設定字元

OUTPUT：\
ch1= G\
ch2= G\
ch3= G

設定變數為某個字元時，字元要以一對單引號 ( ‘ ) 括起來。

在特定的英文字母前，加上反斜線「 \ 」，極為跳脫序列；而反斜線稱為跳脫字元。

| 跳脫序列 | 所代表的意義 | 跳脫序列   | 所代表的意義        |
| ---- | ------ | ------ | ------------- |
| \f   | 換頁     | \\\\   | 反斜線           |
| \b   | 倒退一格   | \’     | 單引號           |
| \n   | 換行     | \”     | 雙引號           |
| \r   | 歸位     | \uxxxx | 16進位unicode字元 |
| \t   | 跳格     | \ddd   | 8進位unicode字元  |

實際帶做個範例：

```
package ch03;

public class ch03_4_2 {

	public static void main(String[] args) {
		char ch1='\"';      //將ch1 設為雙引號
		char ch2='\74';     //以8進位設定字元變數
		char ch3='\u003e';  //以16進位設定字元變數

		System.out.println(ch1+"Example"+ch1);
		System.out.println("\"Example\"");  //無使用變數，直接帶入跳脫序列
		System.out.println(ch2+"Time is money!"+ch3);


		char ch4=074;
		char ch5=0x3e;

		System.out.println(ch4+"Time is money!"+ch5);

	}

}
```

![OUTPUT](/files/-M4gdZ0pjVpUsldsjml6)

變數ch2與ch4效果同；ch3與ch5效果同。\
為變數設值時，若在數值前加上數字 0 ，表示該變數為以八進位值設值；\
若在數值前加上數字 0 與英文字母 x （大小寫均可），則是以16進位值設值。

{% hint style="info" %}
用 0x 表示的16進位值，沒有限制一定要以四個數字的方式輸出。
{% endhint %}

### 三、浮點數

> 帶有小數點的數值稱為實數，在 Java 這種資料型態稱為浮點數。
>
> 若浮點數需要以指數的形式來表示時，可用字母E或e來代表10的乘幂。\
> 例如 6.2e5 代表6.2 x 10^5

| <p><br>float</p> | double          |                        |
| ---------------- | --------------- | ---------------------- |
| 所屬類別             | java.lang.Float | java.lang.Double       |
| 最大值代碼            | MAX\_VALUE      | MAX\_VALUE             |
| 最大值              | 3.4028235E38    | 1.7976931348623157E308 |
| 最小值代碼            | MIN\_VALUE      | MIN\_VALUE             |
| 最小值              | 1.4E-45         | 4.9E-324               |

```
float num1=2.63e64;   //已超過float範圍
float num2=6.3;       //編譯錯誤，浮點數預設型態為double
double num3=6.3;      //編譯正確
```

### 四、布林型態

布林型態只有 ture 和 false，不能用0或1表示。

```
package ch03;

public class ch03_4_3 {

	public static void main(String[] args) {
		boolean status=false;
		System.out.println("status= "+status);

	}

}
```

OUTPUT：\
status= false


---

# 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-03-3.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.
