package 类型转换;
import javax.swing.text.Style;
import java.sql.SQLOutput;
public class Demo1 {
public static void main(String[] args) {
int i = 128;
//进行强制转换 (类型) 变量名
double b =(byte) i; //内存溢出
//double最高不会出现内存溢出
//bype最大值127
//高到低强制转换
// 自动转换 低到高
System.out.println (i);
System.out.println (b);
/*
注意点:
1.不能对布尔值进行转换booleam
2.不能把对象类型转换为不相干的类型 比如:人不能转换成动物,但是可以把男人变成女人的意思。
3.再把高容量转换成低容量的时候,强制转换
4.转换的时候,会出现内存溢出,或者精度问题
*/
System.out.println ("=================");
System.out.println ((int)23.7);//23
System.out.println ((int)-45.8f);//-45 精度.类型都会去除
System.out.println ("=================");
char c = 'a';
int d = c+1;
System.out.println (d);
System.out.println ((char) d);
}
}
public class Demo2 {
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线进行分割
public static void main(String[] args) {
int money = 10_0000_0000;
int yers = 20;
int you = money* yers;
int you2 = money* yers;
long you3 = money* yers;
System.out.println (you3);//默认是int,在转换之前就已经出现问题了?
System.out.println (you);//-1474836480计算溢出
System.out.println (you2);//-1474836480计算溢出
long you4 = money*((long)yers);//先把一个数转换为long
System.out.println (you4);
}
}