## 1.`double`類型

Java 提供了`double`處理實數（小數）的類型。它佔用`8 bytes`內存（是類型的兩倍`int`）並且可以存儲範圍從到 的值。為了比較：該類型可以存儲從到範圍內的值。`-1.7*10308``+1.7*10308``int``-2*109``+2*109`

## 2.創建`double`類型

``double name;``

``double price;``
`price`創建一個真正的變量
``double weight;``
`weight`創建一個真正的變量
``double lightSpeed;``
`lightSpeed`創建一個真正的變量

`int`類型一樣，您可以使用速記同時創建多個`double`變量：

``double name 1, name 2, name 3;``

``double name 1 = value 1, name 2 = value 2, name 3 = value 3;``

``double price = 5.0;``

``double weight = 2;``

``double x = 1.0, y = 2.0, z = 3.0;``

## 3.分配整數和實數

``double price = 5.0;``

``double weight = 2;``

``````int t = 1000;
double x =  t * t;``````

``````int t = 1000;
double x = t * 5.0;``````

``System.out.println(5 * 2);``

``System.out.println(5 * 2.0);``

``integer_variable = (int)(real_number);``

``int x = (int)(5.5);``

``````double a = 5.999;
int x = (int)(a);``````

``````double a = 5.999;
int b = 2;
int x = (int)(a * b);``````

## 4. Java 整數與實數相除

``double d = 5 / 2;``

``double d = 5.0 / 2;``
``double d = 5 / 2.0;``
``double d = 5.0 / 2.0;``

``````int a = 5;
int b = 2;
double d = a / b;``````

``````int a = 5;
int b = 2;
double d = a * 1.0 / b;``````

``````int a = 5;
int b = 2;
double d = 1.0 * a / b;``````
`(1.0 * a) / b;` `2.5`
``````int a = 5;
int b = 2;
double d = a * 1.0 / b;``````
`(a * 1.0) / b;` `2.5`
``````int a = 5;
int b = 2;
double d = a / b * 1.0;``````
`(a / b) * 1.0;` `2.0`