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

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

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



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


Process finished with exit code 0




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