首页 > 编程语言 > 详细

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer 类型转换报错解决

时间:2020-04-14 11:55:42      阅读:53      评论:0      收藏:0      [点我收藏+]

原因:long 和 Integer 之间没有任何继承关系,都继承于Number类

测试代码:

public class test{
    public static void main(String[] args){
        
        long num_long = 2;
        Integer num_integer = 3;
        //long Integer int 转换
        int num1 = 1;
        num1 = num_integer;
        System.out.println("Integer 转 int : "+ num1);
        int num2 = 1;
        num2 = (int)num_long;
        System.out.println("long 转 int : "+ num2);
        num_integer =(Integer)((Long) num_long).intValue();
        System.out.println("long 转 Integer : "+ num_integer);
        long num_long1 = 2;
        Integer num_integer1 = 3;
        num_long1 =((long) num_integer1);
        System.out.println("Integer 转 long : "+ num_long1);
    }
}

输出结果:

Integer 转 int : 3
long 转 int : 2
long 转 Integer : 2
Integer 转 long : 3

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer 类型转换报错解决

原文:https://www.cnblogs.com/hzhq1255/p/12696801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!