-1

As title,java 1.8 reserves the word "_".What use for?

Message:- '_' should not be used as an identifier, since it is a reserved keyword from source level

cfgbd
  • 3
  • 1
  • 4

1 Answers1

5

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you to separate groups of digits in numeric literals, which can improve the readability of your code.

For example, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

The following example shows other ways you can use the underscore in numeric literals:

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

NOTE: You can place underscores only between digits.

You cannot place underscores in the following places:

  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F, D, or L suffix
  • In positions where a string of digits is expected

Following are some valid and invalid examples of underscore placements:

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)

I hope this would satisfy your requirements.

ricky3350
  • 1,710
  • 3
  • 20
  • 35
Nikunj
  • 145
  • 3
  • Thanks for your help,I being satisfied! – cfgbd Jan 03 '16 at 14:02
  • that seems retarded idea, it looks way worse and still don't get why _ can't be used as identifier... – Enerccio Jul 04 '17 at 05:26
  • That's not the reason why `_` got reserved in Java 8. E.g. you still can use identifiers like `__`. Its clear from the context if `_` appears as variable name or in a numeric identifier. The reason is that Java 8 introduced lambda expressions and `_` is ought to be used in a future Java release as placeholder for ignored parameters (which sadly isn't implemented yet). – David Ongaro Nov 20 '17 at 05:44