In Java, variable names start with a letter, currency character ($
) etc. but not with number, :
, or .
Simple question: why is
Simply put, it would break facets of the language grammar.
For example, would 7f
be a variable name, or a floating point literal with a value of 7
?
You can conjure others too: if .
was allowed then that would clash with the member selection operator: would foo.bar
be an identifier in its own right, or would it be the bar
field of an object instance foo
?
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: