Java:什么是 1d?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27368422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: what is 1d?
提问by aralar
Came across the following expression on Java and I have no idea what "1d" means (r
is an integer). There are also optionsfor longs, doubles... What are they and what are they used for?
在 Java 上遇到以下表达式,我不知道“1d”是什么意思(r
是一个整数)。还有多头、双打的选项......它们是什么以及它们的用途是什么?
double y = r / (Integer.MAX_VALUE + 1d);
Thanks!
谢谢!
回答by Yoda
Sufix d
after number means double
, sufix f
means float
.
数字d
后面的double
后缀f
表示,后缀表示float
。
You can express double literal in eight ways: 1.0
, 1d
, 1D
, 1.0d
, 1.0D
, 1.
, 1.d
, 1.D
and not truly double literal: (double)1
. In the last example 1
is the literal, it is an int
but then I cast it to double
.
你可以用八种方式来表达双重文字:1.0
, 1d
, 1D
, 1.0d
, 1.0D
, 1.
, 1.d
,1.D
而不是真正的双重文字:(double)1
。在最后一个例子中1
是文字,它是一个int
但然后我将它转换为double
.
In your expression: double y = r / (Integer.MAX_VALUE + 1d);
parentheses increase priority of expression so the (Integer.MAX_VALUE + 1d)
will be evaluated first and because this is intValue + doubleValue
the outcome will be of type double
so r / (Integer.MAX_VALUE + 1d)
will be also a double
.
在您的表达式中:double y = r / (Integer.MAX_VALUE + 1d);
括号增加了表达式的优先级,因此(Integer.MAX_VALUE + 1d)
将首先评估,因为这是intValue + doubleValue
结果的类型,double
所以r / (Integer.MAX_VALUE + 1d)
也是 a double
。