是否可以同时使用 L 和 E 字母来初始化 'Long' 数字文字?

Is it possible to use both L and E letters to initializing 'Long' numeric literal?

如果我将 long 变量声明为:

long n = 1E+12L;

编译器抛出语法错误。

当我将其声明为:

long n = 1E+12;

它抛出类型不兼容的错误。

另一方面,很高兴接受floats和double中的两个字母:

double n = 1E+12D;
float n = 1E+12F;

为什么它不适用于 long 文字?我每次都必须删除 L 字母并将其转换为 long 吗?

不,这是不可能的。在数字文字中使用 eE(即 scientific notation) is not allowed by the Java Language Specification for integer literals:

IntegerLiteral:
  DecimalIntegerLiteral
  HexIntegerLiteral
  OctalIntegerLiteral
  BinaryIntegerLiteral

DecimalIntegerLiteral:
  DecimalNumeral [IntegerTypeSuffix]

...

IntegerTypeSuffix:
  (one of)
  l L

允许 floating-point literals,但不能有 lL 后缀:

FloatingPointLiteral:
  DecimalFloatingPointLiteral
  HexadecimalFloatingPointLiteral

DecimalFloatingPointLiteral:
  Digits . [Digits] [ExponentPart] [FloatTypeSuffix]
  . Digits [ExponentPart] [FloatTypeSuffix]
  Digits ExponentPart [FloatTypeSuffix]
  Digits [ExponentPart] FloatTypeSuffix

ExponentPart:
  ExponentIndicator SignedInteger

ExponentIndicator:
  (one of)
  e E

...

因此,您要么必须从 double 进行转换,要么必须完整地写出您的 long 文字。不过,为方便起见,您可以使用下划线对数字进行分组,例如 1_000_000_000_000L,以使零的个数清晰。

为什么它对长文字不起作用?

Ee 不允许与 intlong 一起使用。您只能将它们与 floatdouble

一起使用

请检查 tutorial 中的以下文本:

The floating point types (float and double) can also be expressed using E or e (for scientific notation)

我每次都要把L字母丢掉然后投长吗?