Java正确的方式将对象转换/转换为Double [英] Java correct way convert/cast object to Double

查看:629
本文介绍了Java正确的方式将对象转换/转换为Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将对象o转换为Double。是先将其转换为String的正确方法吗?

I need to convert Object o to Double. Is the correct way to convert it to String first?

推荐答案

您不能将对象转换为 Double 如果对象不是Double。

You can't cast an object to a Double if the object is not a Double.

查看 API

特别注意

valueOf(double d);

valueOf(String s);

这些方法为您提供了一种方法从String或double原语获取 Double 实例的过程。 (也不是构造函数;请阅读文档以了解其工作原理)。您尝试自然转换的对象必须为您提供可以转换为double的对象。

Those methods give you a way of getting a Double instance from a String or double primitive. (Also not the constructors; read the documentation to see how they work) The object you are trying to convert naturally has to give you something that can be transformed into a double.

最后,请记住 Double 实例是不可变的-创建后就无法更改它们。

Finally, keep in mind that Double instances are immutable -- once created you can't change them.

这篇关于Java正确的方式将对象转换/转换为Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆