Java双初始化 [英] Java double initialization
问题描述
这些陈述在哪些方面有所不同?
In what way are these statements different?
- double dummy = 0;
- double dummy = 0.0;
- double dummy = 0.0d;
- double dummy = 0.0D;
- double dummy = 0;
- double dummy = 0.0;
- double dummy = 0.0d;
- double dummy = 0.0D;
推荐答案
尝试了一个简单的程序(同时使用0和100,以显示特殊常量和常规常量之间的区别),Sun Java 6编译器将为1和2输出相同的字节码(就编译器而言,情况3和4与2相同)。
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).
例如:
double x = 100;
double y = 100.0;
编译为:
0: ldc2_w #2; //double 100.0d
3: dstore_1
4: ldc2_w #2; //double 100.0d
7: dstore_3
然而,我看不到任何东西Java语言规范保证这种常量表达式的编译时扩展。对于以下情况,编译时缩小:
However, I can't see anything in the Java Language Specification guaranteeing this compile-time widening of constant expressions. There's compile-time narrowing for cases like:
byte b = 100;
在第5.2节,但这并不完全相同。
as specified in section 5.2, but that's not quite the same thing.
也许眼睛比我更敏锐的人可以在某处找到保证...
Maybe someone with sharper eyes than me can find a guarantee there somewhere...
这篇关于Java双初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!