在java中有8种基本数据类型:byte、short、int、long、float、double、char、boolean
精度低的可以向精度高的自动转换、精度高的不能像精度低的转换
byte、short、char不可以相互转换、但是可以运算 运算时会转换成int类型
自动转换规则:
char-->int-->long-->float-->double
byte-->short-->int-->long-->float-->double
实例
public class Test{public static void main(String[] args){//精度低的可以向精度高的转换、精度高的不可以向进度低的转换byte a1=1;short a2=1;int a3=1;long a4=1L;//使用long类型需要加Lchar a5=97;float a6=1.0f;double a7=1.0;boolean a8=true;byte b1=a1+a2;//a1+a2属于int类型 b1属于byte类型 精度高的不可以向精度低的转换错short b2=a3+a1;//a3+a1属于int类型 b2属于short类型 精度高的不可以向精度低的转换 错int b3=a4+a3;//a4+a3属于long类型 b3属于int类型 精度高的不可以向精度低的转换 错long b4=a1+a2;//a1+a2属于int类型 b4属于long类型 精度低可以向精度高的转换 对int b5=a5;//a5属于char类型b5属于int类型 精度低可以向精度高的转换对char b6=a3;//a3属于int类型b6属于char类型 精度高的不可以向精度低的转换 错//char与byte、short不可以相互转换 但是可以相互运算 运算时默认是int类型char b7=a1; //char与(byte、short)不可以相互转换 错short b8=a5; //char与(byte、short)不可以相互转换 错byte b9=a5; //char与(byte、short)不可以相互转换 错short b10=a1+a2;//a1+a2属于int类型 b10属于short类型 错short b11=a1+a5;//a1+a5属于int类型 b11属于short类型错int b12=a1+a5;//a1+a5属于int类型 b12属于int类型 对int b13=a2+a1;//a1+a2属于int类型 b13属于int类型对}}