Java:为什么你需要在浮点数中指定一个'f'? [英] Java: Why do you need to specify an 'f' in a float literal?
本文介绍了Java:为什么你需要在浮点数中指定一个'f'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
f
? 解决方案
因为,否则它默认为 double
使用浮点类型比 float
。
如果后缀为ASCII字母F或f,则浮点类型为float类型;否则它的类型是双精度的,它可以选择性地加上一个ASCII字母D或d(§4.2.3)。
我宁愿没有违约,在所有情况下都要说清楚,但这是另一回事。)
Why do you need to specify a suffix f
in a float literal?
解决方案
Because otherwise it defaults to double
, which is a more commonly used floating point type than float
.
From the Java Language Specification, section 3.10.2:
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (§4.2.3).
(Personally I'd rather there were no default, to make it clear in all cases, but that's a different matter.)
这篇关于Java:为什么你需要在浮点数中指定一个'f'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文