YDOOK:Java: byte 转其他类型 自动类型转化

YDOOK:Java: byte 转其他类型 自动类型转化

© 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));



    //获取变量类型方法
    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=59456: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

Process finished with exit code 0




YDOOK CSDN认证博客专家 算法 人工智能
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页