ClassCastException,将Integer转换为Double [英] ClassCastException, casting Integer to Double
本文介绍了ClassCastException,将Integer转换为Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ArrayList marks = new ArrayList();
Double sum = 0.0;
sum = ((Double)marks.get(i));
每当我尝试运行我的程序时,我得到一个ClassCastException,声明:java.lang.Integer不能被强制转换为java.lang.Double
Everytime I try to run my program, I get a ClassCastException that states: java.lang.Integer cannot be cast to java.lang.Double
推荐答案
我们可以投一个 int
到双击
,但我们不能做与包装类相同整数
和 Double
:
We can cast an int
to a double
but we can't do the same with the wrapper classes Integer
and Double
:
int a = 1;
Integer b = 1; // inboxing, requires Java 1.5+
double c = (double) a; // OK
Double d = (Double) b; // No way.
这显示了与运行时异常相对应的编译时错误。
This shows the compile time error that corresponds to your runtime exception.
这篇关于ClassCastException,将Integer转换为Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文