# 型態轉換

`double PI = 3.14;`

`float PI = 3.14;`

`float PI = 3.14F;`

`float PI = (float) 3.14;`

`int number = 10;`

`int number = 2147483648;`

`long number = 2147483648;`

`long number = 2147483648L;`

`byte number = 10;`

`byte number = 128;`

128超過`byte`可儲存的範圍，於是會使用`int`儲存128，你要將`int`型態儲存至byte變數，就會出現possible loss of precision的編譯錯誤。

```int a = 10; double b = a * 3.14;```

```short a = 1; short b = 2; short c = a + b; // possible loss of precision ```

```short a = 1; short b = 2; short c = (short) (a + b);```

```short a = 1; long b = 2; int c = a + b;  // possible loss of precision```

```short a = 1; long b = 2; int c = (int) (a + b);```

`System.out.println(10 / 3);`

`System.out.println(10.0 / 3);`

```int count = 0; while(someCondition) {```
`    if(count + 1 > Integer.MAX_VALUE) {`
`        count = 0;`
`    }`
`    else {`
`        count++;`
`    }`
`    ...`
`}`

01111111 11111111 11111111 11111111

`count + 1`則會變為：

10000000 00000000 00000000 00000000

Promotion 與 Cast 中還有兩個例子，你可以想想看問題是什麼？