Day05-java的两大类型转换

 强类型语言

要求变量名需要按严格规定执行后才可以用,java就是

 java的数据类型两大类

基本类型 (primitive type)八大基本数据类型

引用类型 (reference type)String等

类型转换

由于java是强类型的语言,所以进行有些运算的时候需要用到类型转换。

运算中,不同类型的数据转换为同一类型,然后进行运算。

低-----高

byte,short,char---int--long--float--double


    类型间的转换,2种方式

        int a =5;
        long b = a;//自动类型转换
        int c = (int)b;//强制类型转换

        long d = 5;//自动类型转换,5是int
        double e = 5;//

        long f =100000000000L;
        int g =(int)f;
        System.out.println(g);//可能会溢出,1215752192

        double h = 27.333;
        int i = (int)h;//可能丢失精度,27
        System.out.println(i);


        //类型间的转换
        byte b1 = 5;//5是直接量不需要,注意范围,变量需要强转
        byte b2 = 6;
        byte b3 =(byte) (b1+b2);//系统运算时候byte,short,char默认转换为int,再运算

        System.out.println(2+2);//4
        System.out.println('2'+2);//52
        System.out.println('2'+'2');//100,'2'的码50加'2'的码50
        System.out.println('2');//2,只有在运算时才转为int,此处并没有运算



        /*long类型注意
       //long类型,很大很大,-21亿-21亿之间,数字后面加L,不加默认int
        //数字很大时候,建议第一个数字加L
        long a = 1000000000L;
        System.out.println(a);
        long b = 1000000000*2*3L;
        System.out.println(b);
        long c = 1000000000*3*3L;//溢出了,-3884901888
        System.out.println(c);
        long d = 1000000000L*3*3;
        System.out.println(d);

         */


        /*double类型注意

        //double 8个字节,浮点型,小数,可能会发生舍入误差,精确数值不建议使用,财务系统,

        double a = 3.14;
        float b = 3.14F;

        double c = 2.0,d=1.9;//0.10000000000000009
        System.out.println(c-d);

         */

        /*布尔型注意

     //boolean 数据类型演示,一个字节,布尔型,
        boolean a = true;//true为boolean的直接量
        boolean b = false;//false为boolean的直接量
       // boolean c = 25;编译错误,布尔型只能赋值为true或false

         */

        /*
        //char类型,2个字符,Unicode编码格式,'a'=97,'A'=65,'0'=48
        //一个码对应一个字符,本质是int,范围在 0-65535之间

        char c1 = '难';
        char c2 = '7';
        char c3 = 'f';
        char c4 = 'F';
        char c5 = '#';
        char c6 = ' ';
        char c7 = '\\';
        //char c10 = 女;编译错误,必须在单引号中
        //char c11 = '';编译错误,必须有字符
        //char c12 = '女婿';编译错误,只能有一个字符

        char c8 = 65 ;//0-65535
        char c9 = 65535;//由于c9是char类型,所以会以char格式输出

    
         */

    }

}
public static void main(String[] args) {
    long a = 123123123123123123L;
    int b = 123;
    short c = 10;
    byte d = 8;
    System.out.println(a+b+c+d);//long
    System.out.println(b+c+d);//int,除了long,其他都是默认转int
    System.out.println(c+d);//int
    System.out.println(d);//int

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值