欢迎光临散文网 会员登陆 & 注册

数据类型自动转化

2022-06-03 13:40 作者:虚云幻仙  | 我要投稿

/**

*测试数据类型自动转换 byte是1个字节 short是2个字节 char是2个字节 int是4个字节 long是8个字节 float是4个字节 double是8个字节

*/

public class TestTypeAutoConvert{

    public static void main(String[]args){

        byte a1 = 60;

        System.out.println(a1);

        short a2 = ++a1;//byte可以自动转short

        int a3 = a2 + a1;//byte short 可以自动转int

        char a4 = 'h';

        int a5 = a4;//char可以自动转int

        long a6 = a5 + a3;//int可以自动转long

        float a7 = a6;//int long 可以自动转float但可能损失精度

        double a8 = a7 + 1;//float int 可以自动转double  ,long自动转double可能损失精度

        System.out.println(a2);

        System.out.println(a3);

        System.out.println(a4);

        System.out.println(a5);

        System.out.println(a6);

        System.out.println(a7);

        System.out.println(a8);

        char b1 = 97+25;//整型常量97 25可以自动转类型,需要满足值不超过所转类型的范围

        System.out.println(b1);

    }

}


数据类型自动转化的评论 (共 条)

分享到微博请遵守国家法律