会计考友 发表于 2012-8-4 12:44:44

JAVA认证:Java基本数据类型转换分析

public class TestConvert {   public static void main(String arg[])
    {
    int i1 = 123;
    int i2 = 456;
    double d1 = (i1+i2)*1.2;//系统将转换为double型运算
    float f1 = (float)((i1+i2)*1.2);//需要加强制转换符
    byte b1 = 67;
    byte b2 = 89;
    byte b3 = (byte)(b1+b2);//系统将转换为int型运算,需
    //要强制转换符
    System.out.println(b3);
    double d2 = 1e200;
    float f2 = (float)d2;    //会产生溢出
    System.out.println(f2);
    float f3 = 1.23f;//必须加f
    long l1 = 123;
    long l2 = 30000000000L;//必须加l
    float f = l1+l2+f3;//系统将转换为float型计算
    long l = (long)f;//强制转换会舍去小数部分(不是四舍五入)
    }
    }
    public class TestConvert2 {

    public static void main(String[] args) {

会计考友 发表于 2012-8-4 12:44:45

JAVA认证:Java基本数据类型转换分析

</p>    int i=1,j=12;
    float f1=(float)0.1; //0.1f
    float f2=123;
    long l1 = 12345678,l2=8888888888L;
    double d1 = 2e20,d2=124;
    byte b1 = 1,b2 = 2,b3 = 127;
    j = j+10;
    i = i/10;
    i = (int)(i*0.1);
    char c1='a',c2=125;
    byte b = (byte)(b1-b2);
    char c = (char)(c1+c2-1);
    float f3 = f1+f2;
    float f4 = (float)(f1+f2*0.1);
    double d = d1*i+j;
    float f = (float)(d1*5+d2);
    }
    }
页: [1]
查看完整版本: JAVA认证:Java基本数据类型转换分析