简体   繁体   English

Java - 将double转换为float

[英]Java - convert double to float

I've seen an example where the following code is used to convert a double to a float: 我已经看到一个示例,其中使用以下代码将double转换为float:

Double.valueOf(someDouble).floatValue()

I would just do it like 我会这样做的

(float)someDouble

Is there any advantage to using the former? 使用前者有什么好处吗?

Looking at the implementation of Double 's floatValue() : 看看DoublefloatValue()

/**
 * Returns the value of this {@code Double} as a {@code float}
 * after a narrowing primitive conversion.
 *
 * @return  the {@code double} value represented by this object
 *          converted to type {@code float}
 * @jls 5.1.3 Narrowing Primitive Conversions
 * @since JDK1.0
 */
public float floatValue() {
    return (float)value;
}

It looks like it behaves exactly like your casting. 看起来它的行为与你的演员完全一样。 Therefore there's no advantage to using it. 因此使用它没有任何好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM