自学内容网 自学内容网

Java基本数据类型转换

一、自动类型转换

1.基本介绍

  1. 当Java程序在进行赋值或者运算时,精度小的类型自动转换为精度大的数据类型,这个就是自动类型转换
  2. 数据类型按精度(容量)大小进行排序为:
    ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/520b6288a88540b0ace9d2c22b844ca4.png在这里插入图片描述
public class AutoConvert {
   public static void main(String[] args) {
       int num = 'a';   // char -> int
       double dom = 80;  // int -> double
       System.out.println(num);  //97
       System.out.println(dom);  // 80.0
   }
}

在这里插入图片描述

2.转换细节

(1)有多种类型的数据混合运算时,系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算。

(2)当我们把精度(容量)大的数据类型赋值给精度(容量)小的 数据类型时,就会报错,反之就会进行自动类型转换。

(3)(byte,short) 和 char 之间不会互相自动转换

(4)byte,short,char 他们三者,(不管是单独还是混合)在计算时首先转换为 int类型。

(5)boolean 不参与类型自动转换

(6)自动提升原则:表达式结果的类型自动提升为 操作数中最大的类型

public class AutoConvertDetail {
    public static void main(String[] args) {
       //细节1:有多种类型的数据混合运算时
       //系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算
       int n1 = 10;
       //float n2 = n1 + 1.1;// 错误的,1.1会默认为 double类型的,结果类型是double
       //double n2 = n1 + 1.1;// 正确的;
       float n2 = n1 + 1.1F; // 正确的,结果类型也是float

        // 细节2:当我们把精度(容量)大的数据类型赋值给精度(容量)小的 数据类型时,就会报错,反之就会进行自动类型转换。


        // 细节3:(byte,short) 和 char 之间不会互相自动转换;
        // 当把具体数 赋给 byte时,(1)先判断该数是否在byte范围内,如果是就可以
        byte b1 = 10; // 对,-128~127

        // int n2 = 1; //n2是int
        // byte b2 = n2; // 错误,原因:如果是变量赋值,判断类型

        // char c1 = b1; //错误,byte不能自动转换成 char
        //

        //细节4:byte,short,char  他们三者可以计算,在计算时首先转换为int 类型

        byte b2 = 1;
        byte b3 = 2;
        short s1 = 1;
        // short s2 = b2 + s1; //错误的,因为 b2 + s1 -> int
        int s2 = b2 + s1; // 正确的

       //byte b4 = b2 + b3; //错误的,b2 + b3 => int


       //细节5:boolean 不参与类型自动转换
       boolean pass = true;
       //int num = pass; // 错误的

       //细节6:自动提升原则:表达式结果的类型自动提升为 操作数中最大的类型
       byte b4 = 1;
       short s3 = 100;
       int num200 = 1;
       double num300 = 1.1;

       double num400 = b4 + s3 + num200 + num300;

    }
}

二、强制类型转换

1.基本介绍

自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符(),但可能造成精度降低或溢出,格外要注意。

public class ForceConvert {
    public static void main(String[] args) {

         //演示强制类型转换
         int n1 = (int)1.9;
         System.out.println("n1 = " + n1);  //1 造成 精度损失

        int n2 = 2000;
        byte b1 = (byte)n2;
        System.out.println("b1 = " + b1);  // 造成 数据溢出
    }
}

2.转换细节

(1)当进行数据的大小 从大——> 小,就需要使用 强制转换

(2)强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级

(3)char 类型可以保存int的常量值,但不能保存int的变量值,需要强转

(4)byte 和 short 类型在进行运算时,当做int 类型处理

public class ForceConvertDetail {
    public static void main(String[] args) {

        //强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级
        //int x = (int)10*3.5 + 6*1.5; //编译错误:double -> int
        int x = (int)(10*3.5 + 6*1.5); //正确,44.0 -> 44
        System.out.println(x);


        char c1 = 100;//OK
        int m = 100; //OK
        //char c2 = m; //编译错误
        char c3 = (char)m; // OK
        System.out.println(c3);// 100对应的字符 d

    }
}

在这里插入图片描述


原文地址:https://blog.csdn.net/asacmxjc/article/details/142729927

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!