数据类型转换:
隐式类型转换:
小类型到大类型会自动转换:
byte->short,char->int->long->float->double
虽然float比long小,但是因为存储方法不一样导致float里面可以存储的数据范围比long要大(浮点型来说,有一部分是用来表示有效位数的,有一部分是用来表示多次方。)
使用情况:
1 、赋值语句
2、算术表达式中
显示类型转换(也叫做强制类型转换)
把一个大类型赋值给小类型,需要进行前置类型转换
int i=(int)3.3;
long j = (long)3.4f;
int c = 'a';//隐式转换
public class TypeCast{
public static void main(String[] args){
byte a = 100;
short b = a;
int c = b ;
c = a ;
long d = c;
float e = d ;
double f=e;
float res = a+e;
double h = 'a';
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(res);
System.out.println(h);
f=200;
a = (byte)f ;
System.out.println(a);
int i =(int)3.4f;
System.out.println(i);
}
}