Reputation: 584
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;
Upvotes: 15
Views: 12952
Reputation: 140417
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:
Upvotes: 22
Reputation: 234635
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
?
Upvotes: 44