public class Demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i; //記憶體溢出
//強制轉換 (類型)變量名
//自動轉換 低到高
System.out.println(i);
System.out.println(b);
/*注意點
1.不能對布爾值轉換
2.不能把對象類型轉換為不相幹的類型
3.在把高容量轉換為低容量的時候,強制轉換
4.轉換到的時候可能存在記憶體溢出,或者精度問題!
*/
System.out.println("====================");
System.out.println((int)23.7);//輸出23
System.out.println((int)-45.89f);//輸出-45
System.out.println("====================");
char c = 'a';//a=97
int d =c+1;
System.out.println(d);
System.out.println((char)d);//生成b b=98
}
public class Demo05 {
public static void main(String[] args) {
//操作比較大的數的時候,注意溢出問題
//jdk7新特性 數字之間可以用下劃線分割
int money = 10_0000_0000;
int years =20;
int total = money*years;//-1474836480,計算溢出
System.out.println(total);
long total2 = money *years;//預設是int類型計算前已經出問題了
long total3 = ((long)money) *years;//先把一個數轉換為long
System.out.println(total3);
}
}