Java双重初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1443331/
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-08-12 12:20:23  来源:igfitidea点击:

Java double initialization

javadoubledeclaration

提问by Alberto Zaccagni

In what way are these statements different?

这些陈述有何不同?

  1. double dummy = 0;
  2. double dummy = 0.0;
  3. double dummy = 0.0d;
  4. double dummy = 0.0D;
  1. 双虚拟 = 0;
  2. 双虚拟 = 0.0;
  3. 双虚拟 = 0.0d;
  4. 双虚拟 = 0.0D;

采纳答案by Jon Skeet

Having tried a simple program (using both 0 and 100, to show the difference between "special" constants and general ones) the Sun Java 6 compiler will output the same bytecode for both 1 and 2 (cases 3 and 4 are identical to 2 as far as the compiler is concerned).

尝试了一个简单的程序(同时使用 0 和 100,以显示“特殊”常量和一般常量之间的区别)后,Sun Java 6 编译器将为 1 和 2 输出相同的字节码(情况 3 和 4 与 2 相同)就编译器而言)。

So for example:

例如:

double x = 100;
double y = 100.0;

compiles to:

编译为:

0:  ldc2_w  #2; //double 100.0d
3:  dstore_1
4:  ldc2_w  #2; //double 100.0d
7:  dstore_3

However, I can't see anything in the Java Language Specification guaranteeingthis compile-time widening of constant expressions. There's compile-time narrowingfor cases like:

但是,我在 Java 语言规范中看不到任何内容来保证常量表达式的这种编译时扩展。对于以下情况,编译时间会缩小

byte b = 100;

as specified in section 5.2, but that's not quite the same thing.

第 5.2 节中所述,但这并不完全相同。

Maybe someone with sharper eyes than me can find a guarantee there somewhere...

也许比我眼睛更敏锐的人可以在某处找到保证......

回答by Sean Owen

The last 3 should be identical. The literal on the right side is a double already. The 'd' or 'D' is implicit when you have a decimal literal.

最后3个应该是相同的。右侧的文字已经是双倍了。当您有十进制文字时,'d' 或 'D' 是隐式的。

The first one is slightly different in that 0 is an int literal, which will be widened to a double. I don't know if that even produces different byte code in this case or not; the result should be identical anyway.

第一个略有不同,因为 0 是一个 int 字面量,它将被加宽为 double。我不知道在这种情况下是否会产生不同的字节码;无论如何,结果应该是相同的。

回答by ante.sabo

for Java I do not know exactly, in C this can be really dangerous if you omit this D at the end since it will not change upper bytes, which can have effect that in your variable lies number which you actually did not put in!

对于 Java,我不完全清楚,在 C 中,如果您在末尾省略此 D,这可能会非常危险,因为它不会更改高字节,这可能会影响您的变量中包含您实际上没有输入的数字!

In Java I had a really big problem with instatntiating BigDecimal - new BigDecimal(0) and new bigDecimal(0L) is NOT the same thing, you can feel it if you migrate your code from Java 1.4 to Java 1.5. Don't know why they were sloppy about it, maybe they had to do it that way.

在 Java 中,我在设置 BigDecimal 时遇到了一个非常大的问题 - new BigDecimal(0) 和 new bigDecimal(0L) 不是一回事,如果您将代码从 Java 1.4 迁移到 Java 1.5,您会感觉到这一点。不知道他们为什么这么草率,也许他们不得不那样做。

回答by Jesper

For the first one:

对于第一个:

double dummy = 0;

the integer literal 0is converted to a double with a widening primitive conversion, see 5.1.2 Widening Primitive Conversionin the Java Language Specification. Note that this is done entirely by the compiler, it doesn't have any impact on the produced bytecode.

整数文字0通过扩展原语转换转换为双精度数,请参阅Java 语言规范中的5.1.2扩展原语转换。请注意,这完全由编译器完成,它对生成的字节码没有任何影响。

For the other ones:

对于其他人:

double dummy = 0.0;
double dummy = 0.0d;
double dummy = 0.0D;

These three are exactly the same - 0.0, 0.0dand 0.0Dare just three different ways of writing a doubleliteral. See 3.10.2 Floating-Point Literalsin the JLS.

这三个完全相同 - 0.00.0d并且0.0D只是编写double文字的三种不同方式。请参阅JLS 中的3.10.2 浮点文字