[英]Dynamic casting. ClassCastException
everybody =) I'm trying to cast a string to Double this way 所有人=)我正在尝试以这种方式将字符串转换为Double
Class.forName("java.lang.Double").cast("100")
But it throws ClassCastException. 但是它抛出ClassCastException。 :(
:(
What's wrong with this? 这怎么了 And how can I achieve what I want.
以及如何实现我想要的。
You cannot cast a String
do Double
. 您不能将
String
Double
。 If you want to get the Double
representation for a String
, you can either: 如果要获取
String
的Double
表示形式,则可以:
Double d = new Double("100");
or 要么
double d = Double.parseDouble("100");
You're essentially trying to cast a String to Double like so 您实际上是想像这样将String转换为Double
String s = "100";
Double d = (Double)s;
You can't do that. 你不能那样做。 This is the reason for your ClassCastException.
这就是您的ClassCastException的原因。
You need to use Double.valueOf() 您需要使用Double.valueOf()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.