java数据类型转换

来源:互联网 发布:淘宝店铺装修图库 编辑:程序博客网 时间:2024/06/05 08:56

 java类型之间的转换:

首选是精度的顺序:byte.short.char<int<long<float<double

byte.short.char是一个层次的,相互之间不转换,但用时全部转换成int型
精度高的转换成精度低的时,要强制转化,否则不用
如:double i=1.0;
  float j=(float)i;
  double a=j;
有一些注意的:
float f=0.1f;是正确的
但是float f=0.1;错误
又如:
byte b1=1;
byte b2=2;
byte b3=b1+b2; 是错误的
要写成: byte b3=(byte)(b1+b2);
因为byte运用时,系统将byte转化成int型

原创粉丝点击