加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 数字文字后缀大小写的编码指南

发布时间:2020-12-15 05:03:24 所属栏目:Java 来源:网络整理
导读:我做了一些googleling,但无法找到我的问题的解决方案. 根据double和float声明,是否有任何java编码指南? 什么是正确的? Double d = 1.0d; 要么 Double d = 1.0D; (同样适用于Float和Long以及它们的原语.) 编译和两者似乎对我都没问题,但是在阅读这些代码行
我做了一些googleling,但无法找到我的问题的解决方案.

根据double和float声明,是否有任何java编码指南?
什么是正确的?

Double d = 1.0d;

要么

Double d = 1.0D;

(同样适用于Float和Long以及它们的原语.)

编译和两者似乎对我都没问题,但是在阅读这些代码行时,我看到了清晰度的差异.具有大写d的那个似乎更有可能被读错 – > “D”可以读作“0”左右.

有什么建议或想法吗?

解决方法

从 the Java Tutorials开始:

Floating-Point Literals

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation),F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;
// same value as d1,but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;

这告诉你:

> D和d都具有相同的含义 – 正如您所观察到的那样
>“按惯例[D或d被省略]”因为它们是默认值

“什么是更好”类型的问题在StackOverflow上是offtopic,因为它们不能有正确或不正确的答案,只有意见.

但是如果你要求一个约定我会倾向于遵循我上面发现的 – 省略后缀.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读