我看过一个示例,其中使用以下代码将 double 转换为 float :
Double.valueOf(someDouble).floatValue()
我会这样做
(float)someDouble
使用前者有什么优势吗?
最佳答案
查看 Double
的 floatValue()
的实现:
/**
* 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;
}
看起来它的行为与您的类型转换完全一样。因此,使用它没有任何优势。
关于java - 将 double 转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42109916/