Java Unicode variable names

前端 未结 2 1937
深忆病人
深忆病人 2020-12-03 17:34

I got into an interesting discussion in a forum where we discussed the naming of variables.

Conventions aside, I noticed that it is legal for a variable to have the

2条回答
  •  余生分开走
    2020-12-03 17:46

    The Unicode standard defines what counts as a letter.

    From the Java Language Specification, section 3.8:

    Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in their programs that are written in their native languages.

    A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true. A "Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart(int) returns true.

    From the Character documenation for isJavaIdentifierPart:

    Determines if the character (Unicode code point) may be part of a Java identifier as other than the first character. A character may be part of a Java identifier if any of the following are true:

    • it is a letter
    • it is a currency symbol (such as '$')
    • it is a connecting punctuation character (such as '_')
    • it is a digit
    • it is a numeric letter (such as a Roman numeral character)
    • it is a combining mark
    • it is a non-spacing mark
    • isIdentifierIgnorable(codePoint) returns true for the character

提交回复
热议问题