我们是否应该在double,java中的float值结束时添加“ d&quot”。

发布于 2025-02-10 02:21:26 字数 430 浏览 2 评论 0原文

在定义双重和浮动变量时,我通常会感到困惑,据我所知,在双重浮点值的末尾添加“ D”,“ F”等是一个很好的做法。但是,由于IDE没有警告我,通常我会省略它们,并认为这也可以,也无需添加这些字母。关于这些变量:

1。除了在Java中添加了“ D”和“ F”的字母?

2。为什么我们在浮点和双变量定义的末尾添加这些字母?我们应该使用还是不使用?

3。在定义这些变量(除列表,地图等)时,如下所示,我应该喜欢它们的原始或包装类版本吗?

我只是简单地定义了一个类似:

double quantityAmount = 200d;

// or

double quantityAmount = 200;

您能澄清我这个问题吗?

I am generally confused when defining double and float variables and as far as I know, it is good practice to add "d", "f", etc. at the end of double, float values. However, as the IDE does not warn me, generally I omit them and think it would also be ok and no need to add these letters. Regarding to these variables:

1. Is there any letter except from "d" and "f" added to the variable definitions (double, float) in Java?

2. Why do we add these letters at the end of float and double variable definitions? And should we use or not?

3. When defining these variables (except from List, Map, etc.) as shown below, should I prefer primitive or wrapper class version of them?

I simply define a double like:

double quantityAmount = 200d;

// or

double quantityAmount = 200;

Could you please clarify me about this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2025-02-17 02:21:26
  1. 是的。阅读完整列表的官方语言文档! https://docs.oracle。 com/javase/specs/jls/se11/html/jls-3.html#jls-3.10
  2. 为什么:在需要指定类型时表示类型;例如,假设您具有一个超载功能,该功能具有一个用于INT的实现,一个用于double;如果您通过300,它将调用int版本,如果您通过300D,它将调用双版本。
  3. 偏好是个人的事情,我无法告诉您您喜欢什么。从功能上讲,两者都是具有琐碎数据类型的恒定初始化,并且(bytecode-bitwise!)完全相同。
  1. yes. Read the official language documentation for a full list! https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.10
  2. Why: to denote the type when you need to specify the type; for example, say you have an overloaded function that has one implementation for int, and one for double; if you pass 300, it will call the int version, if you pass 300d, it will call the double version.
  3. Preference is a personal thing, I can't tell you what you like. Functionally, both are constant initializations with a trivial data type and do (bytecode-bitwise!) exactly the same.
失眠症患者 2025-02-17 02:21:26
  1. 对于float(f或f)和double(d或d)以获取更多检查(默认值): https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 。浮点类型(浮点和双重)也可以使用E或E(对于科学符号),F或F(32位浮动字面)和D或D(64位双文字;这是默认值和D或D)表示。约定被省略)。
  2. 如果浮点字体的字体为float,则它以字母f或f结尾;否则,它的类型是双重的,并且可以选择以字母d或d结束。
  3. 那取决于你。 :)
  1. for float(F or f) and double(D or d) for more check (Default Values):https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html. 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).
  2. 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.
  3. that depends on you. :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文