只适用与基本数据类型
1:自动转换
a:类型兼容 b: 小转大(不需要手动转换)
例如:short s =24; int i =s;
因为short 的字节数是:2;int的字节数是4;
2:强制转换:
a:类型兼容。b:大转小(需要手动转换),会损失精度
例如:int i =2;short s =(short)i;
package day_05_25;
/*
* 数据类型转换问题:
* 一般来说,我们在做运算的时候,需要运算的数据类型必须保持一致
*
* 注意:boolean类型不能转换成其他类型
*
* 默认(自动转换):从小到大:
* A:byte-->short,char-->int,float-->long,double
* B: byte,short,char,相互不能转换,如果他们想参与运算,必须先转换
* 成int类型。
*
* 强制转换:
* 从大的数据类型转换成小的数据类型
* 格式:目标数据类型 变量名 = (目标数据类型)被转换的数据。
*
* 注意:不要随便的去使用强制转换,因为会损失精度。
*/
public class Demo_03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x =3;
int y =5;
System.out.println(x+y);
byte a = 3;
int b = 4;
byte c = (byte)(a+b);
System.out.println(c);
int i = 20013;
char ch='海';
i=ch;
System.out.println(i);
double d=2.23232;
i=(int)d;
System.out.println(i);
}
}