各类数值型数据间的混合运算Java认证考试
文章作者 100test 发表时间 2010:03:13 17:32:53
来源 100Test.Com百考试题网
自动类型转换
整型、实型、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。转换从低级到高级,如下图:
转换规则为:
① (byte或 short) op int→ int
② (byte或 short或 int) op long→ long
③ (byte或 short或 int或 long) op float→ float
④ (byte或 short或 int或 long或 float) op double→ double
⑤ char op int→ int
其中,箭头左边表示参与运算的数据类型,op为运算符(如加、减、乘、除等),右边表示转换成的进行运算的数据类型。
例2.2
public class Promotion{
public static void main( String args[ ] ){
byte b=10.
char c= a .
int i=90.
long l=555L.
float f=3.5f.
double d=1.234.