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

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

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


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

----------------------------------------------------
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


Process finished with exit code 0




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