Search code examples
java

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


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;

Solution

  • 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.