java - 还有什么允许的特殊 java 方法名称?

标签 java jls

我知道 $ 可以用作 Java 中的方法名。还有什么?

class MyClass {
    String $() {
        return "I've never been expected anyone would invoke me";
    }
}

这其实是一个实际问题。我正在寻找除 $ 之外的一个。

最佳答案

方法名称必须是有效的标识符。请参阅 Java 语言规范:3.8 Identifiers什么是有效标识符。

标识符必须以“Java 字母”开头,其余部分必须由“Java 字母或数字”字符组成。

JLS 说:

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

The "Java digits" include the ASCII digits 0-9 (\u0030-\u0039).

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.

请特别注意,它说您不应在源代码中自己使用 $(即使它是允许的字符)。

关于java - 还有什么允许的特殊 java 方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27331435/

相关文章:

java.lang.ClassNotFoundException : net. corda.testing.core.DummyCommandData

java - 为什么 Java 在数字提升期间将字节和短操作数转换为整数

Java:如何将二维 ArrayList 传递给方法/构造函数

java - 使用maven和intellij idea编译java app的区别

java - JLS 关于方法覆盖和泛型是否完整?

java - JLS 是否需要内联最终字符串常量?

java - 不需要 Java 导入的情况(不寻常的资格)[问题已编辑]

java - Java Janguage 规范的澄清

java - java.net.URL 和 java.net.URLConnection 的奇怪问题

java 8 减少累加器返回类型