Java:为什么需要在浮点文字中指定“f"? [英] Java: Why do you need to specify an 'f' in a float literal?

查看:26
本文介绍了Java:为什么需要在浮点文字中指定“f"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要在浮点字面量中指定后缀 f?

Why do you need to specify a suffix f in a float literal?

推荐答案

因为否则默认为double,这是比float更常用的浮点类型.

Because otherwise it defaults to double, which is a more commonly used floating point type than float.

来自 Java 语言规范,第 3.10.2 节:

如果一个浮点文字以 ASCII 字母 F 或 f 为后缀,则它是 float 类型;否则它的类型是 double 并且可以选择后缀为 ASCII 字母 D 或 d(第 4.2.3 节).

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屋!

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