什么用于java1.8" _"保留关键字

时间:2016-01-02 13:48:14

标签: java keyword

作为标题,java 1.8保留单词" _"。用于什么?

消息: - ' _'不应该用作标识符,因为它是源级别的保留关键字

1 个答案:

答案 0 :(得分:4)

在Java SE 7及更高版本中,任何数量的下划线字符(_)都可以出现在数字文字中的数字之间的任何位置。此功能使您可以分隔数字文字中的数字组,这可以提高代码的可读性。

例如,如果您的代码包含具有多个数字的数字,则可以使用下划线字符分隔三个组中的数字,类似于使用逗号或空格等标点符号作为分隔符的方式。

以下示例显示了在数字文字中使用下划线的其他方法:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;

注意:您只能在数字之间放置下划线。

您不能在以下位置放置下划线:

  • 数字的开头或结尾
  • 与浮点字面值中的小数点相邻
  • FDL后缀之前
  • 之前
  • 在预期数字字符串的位置

以下是一些有效且无效的下划线展示位置示例:

float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point

int x1 = _52;              // This is an identifier, not a numeric literal
int x2 = 5_2;              // OK (decimal literal)
int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2;        // OK (decimal literal)

我希望这能满足您的要求。