Why can't Java variable names start with a number?

Malinda :

In Java, variable names start with a letter, currency character ($) etc. but not with number, :, or .

Simple question: why is that?

Why doesn't the compiler allow to have variable declarations such as

int 7dfs;
GhostCat salutes Monica C. :

Because the Java Language specification says so:

IdentifierChars:

JavaLetter {JavaLetterOrDigit}

So - yes, an identifier must start with a letter; it can't start with a digit.

The main reasons behind that:

  • it is simply what most people expect
  • it makes parsing source code (much) easier when you restrict the "layout" of identifiers; for example it reduces the possible ambiguities between literals and variable names.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=441262&siteId=1