基本数据类型之间地转换

本文介绍了Java中基本数据类型的转换规则,包括容量小的数据类型转换为大的类型时不发生溢出,但容量大的转小可能会溢出或精度损失。示例代码展示了byte、short、int、long、float、double之间的转换以及char的特殊处理。
摘要由CSDN通过智能技术生成

byte short int long
floot double
char 虽然是字符,但是即使unico编码中的一个十进制整数,可以当做整数对待

boolean 不能和其他其他七种类型进行转换 true false

转换规则


容量小的转容量大的
byte 1字节 127  --  short 2字节
容量大的转容量小的
int 4字节 129  --  byte 1字节

floot4字节,但是由于小鼠的二进制存储与整数二进制存储结构不同,
4字节floot大于4字节的int,大于8字节的long
byte->short->char->int->long->->float->double

public class Demo {

    public static void main(String[] args) {
        //容量小的转容量大的  可以直接而进行转换
            byte a = 127;
            int b = a;
            long c = b;
            float d = c;
        //容量大的转容量小的
        //大转小:出现溢出
            int m = 258;//2  658 --> -110
            byte n = (byte)m;//int --> byte 编译器不能通过
        System.out.println(n);
        float x = 10.5f;
        //大转小:精度损失
        long y = (long)x;//10
        System.out.println(y);

        char z = 'a';// char也是可以参与转换
        int zc = z;

          byte aa = 10;
          byte bb = 10;
          byte cc = (byte)(aa+bb);// byte short char 在混合运算时,都会上升为int

        float dd = x + m + a;//混合运算时小类型默认都会转为最大的类型
        double xx = x + m + a + 5.5;

        System.out.println(xx);
        System.out.println(dd);
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值