Java中的自动类型转换 & 强制类型转换


什么时候会发生类型转换?

答: 赋值 | 运算时 ,两边数据类型不一致时就会发生类型转换

如下:

public class TypeTest {
    public static void main(String[] args){
       	// 运算时发生的隐式类型转换,两整数相除得到的还是一个整数
        byte a  = 3;
        byte b = 4;
        int num  =  a + b;
        System.out.println(num); // 7
        // 赋值时发生的隐式类型转换
        int ch = '0';
        System.out.println(ch); // 48
        // 运算时发生的强制类型转换
        byte a1 = 12;
        byte a2 = 12;
        byte num1 = (byte)(a1  + a2);
        System.out.println(num1); // 24
        // 赋值时发生的强制类型转换
        short b3 = 1234;
        byte a3 = (byte) b3;
        System.out.println(a3); // -46
    }
}

运行截图:
在这里插入图片描述


类型转换分类:

  1. 自动类型转换
  2. 强制类型转换

自动类型转换(隐式类型转换)

规则:从小到大 ,低字节向高字节自动提升

顺序:

  1. byte(1字节) – > short(2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)

  2. char (2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)

画图分析:
在这里插入图片描述

代码展示:

public class TypeDemo {
    public static void main(String[] agrs){
        // byte -- > short
        byte b1 = 127;
        short s1 = b1;
        System.out.println(s1); // 127
        // short -- > int 
        short  s2 = 30000;
        int i = s2;
        System.out.println(i); // 30000
        // int  -- > long
        int num = 2100000000;
        long lg = num;
        System.out.println(num); // 2100000000
        // long -- > float 
        long lg1 = 200000000000000L;
 	    float f1 = lg1;
        System.out.println(f1);// 2.00000001E14
        // float -- > double 
        float f2 = 3.14f;
        double d1 = f2;
 	    System.out.println(d1); // 3.140000104904175
        // char -- > int
	    char ch = 'a';
        int i1 = ch ;
        System.out.println(i1); // 97
        // char -- > long
        char ch1 = 'b';
        long lg2 = ch1;
        System.out.println(lg2); // 98
        // char  -- >  double
        char ch2 = 'c';
        double dou = ch2;
        System.out.println(dou); // 99.0
        // char -- > float
        char ch3 = 'd';
        float  f3 = ch3;
        System.out.println(f3); // 100.0
    }
}

运行截图:
在这里插入图片描述

注意:

  1. byte、short不能和char进行相互转换

    代码展示:

    public class TypeDemo2 {
        public static void main(String[] agrs){
     	    // byte -- > char
            byte bt = 127;
            char ch = bt;
            System.out.println(ch);
            // short -- > char
            short sh = 12;
            char ch1 = sh;
            System.out.println(ch1);
        }
    }
    

    编译错误截图:
    在这里插入图片描述

  2. float虽然是4个字节,但是float比long表示的数据范围更大。说明数据范围的大小和字节的大小不一定相关

    代码展示:

    public class TypeDemo3 {
        public static void main(String[] agrs){
            long lg = 20000000000000L;
            float f1 = lg;
            System.out.println(f1); // 1.99999997E13
        }
    }
    

    运行截图:
    在这里插入图片描述

  3. boolean类型不能参与类型转换

    代码展示:

    public class TypeDemo4 {
        public static void main(String[] agrs) {
            boolean flag = 12;
            int flag1 = flag;
            System.out.println(flag1);
        }
    }
    

    编译错误截图:
    在这里插入图片描述


强制类型转换(显式类型转换)

规则:从大到小,高字节向低字节手动强制转换

顺序:

  1. double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > short (2字节)-- > byte(1字节)

  2. double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > char(2字节)

画图分析:
在这里插入图片描述

(掌握)格式:目标数据类型 变量名 = (目标数据类型) 变量 | 常量;

代码展示:

public class TypeDemo5 {
    public static void main(String[] agrs){
        // float -- > long
        // final float  PI = 3.14f;
        // long num = (long) PI; // 3
        // float little =  3.14f;
        // long num = (long)little; // 3
 	    long num = (long)3.14f;      
        System.out.println(num);// 3 
        // double -- > float 
        // double dou = 3.14;
        // float little1 = (float)dou; // 3.14
        //  float little1 = (float) 3.14d;  // 3.14
        final double  dou = 3.14;
        float little1 = (float)dou;
        System.out.println(little1); // 3.14
        // long -- > int 
        // long  num1 = 2000000000000L;
        // int   num2 = (int)num1;  // -1454759936
        // int num2 = (int)2000000000000L; // -1454759936
       	final  long num1 = 2000000000000L;
        int num2 = (int)num1;
        System.out.println(num2);  // -1454759936
        // int --> short
        // int  num3  = 12;
        // short num4 = (short)num3; // 12
        // short num4 = (short)40000; // -25536
        final int num3 = 60;
        short num4 = (short)num3;
        System.out.println(num4); // 60
        // short -- > byte
        final short sh = 12345;
        byte bt = (byte)sh;
        System.out.println(bt); // 57
        short sh1 = 78;
	    bt = (byte) sh1;
        System.out.println(bt); // 78
    }
}

运行截图:
在这里插入图片描述

注意:

  1. 强制类型转换有数据丢失,一般不建议使用

    代码展示:

    public  class TypeDemo6 {
       public static void main(String[] agrs) {
           short a = 1245;
           byte b = (byte)a;
           System.out.println(b);
       } 
    }
    

    运行截图:
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔走中的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值