YDOOK:Java: 各大数据类型 自动类型转化

YDOOK:Java: 各大数据类型 自动类型转化

© YDOOK JY Lin



1. 测试 java 程序:

package main;

public class JY2 {

    public static void main(String[] args){
        System.out.println("Java 自动类型转化: YDOOK JY Lin:");

//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("byte => short");
        byte byteVar = 127;
        short shortVar = byteVar;
        System.out.println("byteVar = " + byteVar);
        System.out.println("shortVar = " + shortVar);
        System.out.println("byteVar getType = " + getType(byteVar));
        System.out.println("shortVar getType = " + getType(shortVar));

        System.out.println("----------------------------------------------------");
        System.out.println("byte => int");
        byte byteVar2 = 127;
        int intVar = byteVar2;
        System.out.println("byteVar2 = " + byteVar2);
        System.out.println("intVar = " + intVar);
        System.out.println("byteVar2 getType = " + getType(byteVar2));
        System.out.println("intVar getType = " + getType(intVar));

        System.out.println("----------------------------------------------------");
        System.out.println("byte => long");
        byte byteVar3 = 127;
        long longVar = byteVar3;
        System.out.println("byteVar3 = " + byteVar3);
        System.out.println("longVar = " + longVar);
        System.out.println("byteVar3 getType = " + getType(byteVar3));
        System.out.println("longVar getType = " + getType(longVar));

        System.out.println("----------------------------------------------------");
        System.out.println("byte => float");
        byte byteVar4 = 127;
        float floatVar = byteVar4;
        System.out.println("byteVar4 = " + byteVar4);
        System.out.println("floatVar = " + floatVar);
        System.out.println("byteVar4 getType = " + getType(byteVar4));
        System.out.println("floatVar getType = " + getType(floatVar));

        System.out.println("----------------------------------------------------");
        System.out.println("byte => double");
        byte byteVar5 = 127;
        double doubleVar = byteVar5;
        System.out.println("byteVar5 = " + byteVar5);
        System.out.println("doubleVar = " + doubleVar);
        System.out.println("byteVar5 getType = " + getType(byteVar5));
        System.out.println("doubleVar getType = " + getType(doubleVar));


//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("short => int");
        short shortVar2 = 32767;
        int intVar2 = shortVar2;
        System.out.println("shortVar2 = " + shortVar2);
        System.out.println("intVar2 = " + intVar2);
        System.out.println("shortVar2 getType = " + getType(shortVar2));
        System.out.println("intVar2 getType = " + getType(intVar2));

        System.out.println("----------------------------------------------------");
        System.out.println("short => long");
        short shortVar3 = 32767;
        long longVar2 = shortVar3;
        System.out.println("shortVar3 = " + shortVar3);
        System.out.println("longVar2 = " + longVar2);
        System.out.println("shortVar3 getType = " + getType(shortVar3));
        System.out.println("longVar2 getType = " + getType(longVar2));

        System.out.println("----------------------------------------------------");
        System.out.println("short => float");
        short shortVar4 = 32767;
        float floatVar2 = shortVar4;
        System.out.println("shortVar4 = " + shortVar4);
        System.out.println("floatVar2 = " + floatVar2);
        System.out.println("shortVar4 getType = " + getType(shortVar4));
        System.out.println("floatVar2 getType = " + getType(floatVar2));

        System.out.println("----------------------------------------------------");
        System.out.println("short => double");
        short shortVar5 = 32767;
        double doubleVar2 = shortVar5;
        System.out.println("shortVar5 = " + shortVar5);
        System.out.println("doubleVar2 = " + doubleVar2);
        System.out.println("shortVar5 getType = " + getType(shortVar5));
        System.out.println("doubleVar2 getType = " + getType(doubleVar2));


//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("int => long");
        int intVar3 = 32767;
        long longVar3 = intVar3;
        System.out.println("intVar3 = " + intVar3);
        System.out.println("longVar3 = " + longVar3);
        System.out.println("intVar3 getType = " + getType(intVar3));
        System.out.println("longVar3 getType = " + getType(longVar3));

        System.out.println("----------------------------------------------------");
        System.out.println("int => float");
        int intVar4 = 32767;
        float floatVar3 = intVar4;
        System.out.println("intVar4 = " + intVar4);
        System.out.println("floatVar3 = " + floatVar3);
        System.out.println("intVar4 getType = " + getType(intVar4));
        System.out.println("floatVar3 getType = " + getType(floatVar3));

        System.out.println("----------------------------------------------------");
        System.out.println("int => double");
        int intVar5 = 32767;
        double doubleVar3 = intVar5;
        System.out.println("intVar5 = " + intVar5);
        System.out.println("doubleVar3 = " + doubleVar3);
        System.out.println("intVar5 getType = " + getType(intVar5));
        System.out.println("doubleVar3 getType = " + getType(doubleVar3));


//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("long => float");
        long longVar4 = 2147483647;
//        long longVar4 = 2147483648; long 转化为 float 所取整数不能超过 float 对应的32位二进制整数满值:2147483647!
        float floatVar4 = longVar4;
        System.out.println("longVar4 = " + longVar4);
        System.out.println("floatVar4 = " + floatVar4);
        System.out.println("longVar4 getType = " + getType(longVar4));
        System.out.println("floatVar4 getType = " + getType(floatVar4));


//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("long => double");
        long longVar5 = 2147483647;
//        long longVar5 = 2147483648; long 转化为 float 所取整数不能超过 float 对应的32位二进制整数满值:2147483647!
        double doubleVar4 = longVar5;
        System.out.println("longVar5 = " + longVar5);
        System.out.println("doubleVar4 = " + doubleVar4);
        System.out.println("longVar5 getType = " + getType(longVar5));
        System.out.println("doubleVar4 getType = " + getType(doubleVar4));


//        ========================================================================================
//        ========================================================================================
        System.out.println("----------------------------------------------------");
        System.out.println("float => double");
        float floatVar5 = 2147483647;
        //        long longVar5 = 2147483648; long 转化为 float 所取整数不能超过 float 对应的32位二进制整数满值:2147483647!
        double doubleVar5 = floatVar5;
        System.out.println("floatVar5 = " + floatVar5);
        System.out.println("doubleVar5 = " + doubleVar5);
        System.out.println("floatVar5 getType = " + getType(floatVar5));
        System.out.println("doubleVar5 getType = " + getType(doubleVar5));
    }

    //获取变量类型方法
    public static String getType(Object o){
        return o.getClass().toString();
    }
}


2. 运行结果:

D:\SDK\bin\java.exe "-javaagent:D:\IDEAU\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=62218:D:\IDEAU\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\SpringBoot\java1\out\production\java1 main.JY2
Java 自动类型转化: YDOOK JY Lin:
----------------------------------------------------
byte => short
byteVar = 127
shortVar = 127
byteVar getType = class java.lang.Byte
shortVar getType = class java.lang.Short
----------------------------------------------------
byte => int
byteVar2 = 127
intVar = 127
byteVar2 getType = class java.lang.Byte
intVar getType = class java.lang.Integer
----------------------------------------------------
byte => long
byteVar3 = 127
longVar = 127
byteVar3 getType = class java.lang.Byte
longVar getType = class java.lang.Long
----------------------------------------------------
byte => float
byteVar4 = 127
floatVar = 127.0
byteVar4 getType = class java.lang.Byte
floatVar getType = class java.lang.Float
----------------------------------------------------
byte => double
byteVar5 = 127
doubleVar = 127.0
byteVar5 getType = class java.lang.Byte
doubleVar getType = class java.lang.Double
----------------------------------------------------
short => int
shortVar2 = 32767
intVar2 = 32767
shortVar2 getType = class java.lang.Short
intVar2 getType = class java.lang.Integer
----------------------------------------------------
short => long
shortVar3 = 32767
longVar2 = 32767
shortVar3 getType = class java.lang.Short
longVar2 getType = class java.lang.Long
----------------------------------------------------
short => float
shortVar4 = 32767
floatVar2 = 32767.0
shortVar4 getType = class java.lang.Short
floatVar2 getType = class java.lang.Float
----------------------------------------------------
short => double
shortVar5 = 32767
doubleVar2 = 32767.0
shortVar5 getType = class java.lang.Short
doubleVar2 getType = class java.lang.Double
----------------------------------------------------
int => long
intVar3 = 32767
longVar3 = 32767
intVar3 getType = class java.lang.Integer
longVar3 getType = class java.lang.Long
----------------------------------------------------
int => float
intVar4 = 32767
floatVar3 = 32767.0
intVar4 getType = class java.lang.Integer
floatVar3 getType = class java.lang.Float
----------------------------------------------------
int => double
intVar5 = 32767
doubleVar3 = 32767.0
intVar5 getType = class java.lang.Integer
doubleVar3 getType = class java.lang.Double
----------------------------------------------------
long => float
longVar4 = 2147483647
floatVar4 = 2.14748365E9
longVar4 getType = class java.lang.Long
floatVar4 getType = class java.lang.Float
----------------------------------------------------
long => double
longVar5 = 2147483647
doubleVar4 = 2.147483647E9
longVar5 getType = class java.lang.Long
doubleVar4 getType = class java.lang.Double
----------------------------------------------------
float => double
floatVar5 = 2.14748365E9
doubleVar5 = 2.147483648E9
floatVar5 getType = class java.lang.Float
doubleVar5 getType = class java.lang.Double

Process finished with exit code 0




YDOOK CSDN认证博客专家 算法 人工智能
欢迎访问个人网站: www.ydook.com
相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页