Java Unicode 变量名

标签 java variables unicode

我在讨论变量命名的论坛中进行了一次有趣的讨论。

抛开惯例,我注意到变量具有 Unicode 字符的名称是合法的,例如以下是合法的:

int \u1234;

但是,例如,如果我给它命名#,它会产生错误。根据Sun's tutorial如果“以字母、美元符号“$”或下划线字符“_”开头,则有效。

但是 unicode 1234 是一些埃塞俄比亚字符。那么真正定义为“字母”的是什么?

最佳答案

Unicode 标准定义了什么算作字母。

来自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.

来自isJavaIdentifierPartCharacter 文档:

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

关于Java Unicode 变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422655/

相关文章:

php MYSQL 查询 "ORDER BY"不工作

C++ 在主函数中初始化静态变量

来自 cStringIO 的 Python "\x00"填充/utf-32 字符串

android - Android 中 textview 上的不同语言

java - 一一按下按钮

java - 如何用java读取HDD SMART

java - Android OrientationEventListener 进行多次调用

c# - 如何将变量转换为与动态变量相同的类型?

string - 在 VBA 中获取OpenFileNameW?

Java Webapp 无法在 openshift 上连接 mysql