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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:41:07  来源:igfitidea点击:

Java: what is 1d?

javadouble

提问by aralar

Came across the following expression on Java and I have no idea what "1d" means (ris 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 dafter number means double, sufix fmeans 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.Dand not truly double literal: (double)1. In the last example 1is the literal, it is an intbut then I cast it to double.

你可以用八种方式来表达双重文字:1.0, 1d, 1D, 1.0d, 1.0D, 1., 1.d1.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 + doubleValuethe outcome will be of type doubleso 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