java java中变量的命名限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12857340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:30:33  来源:igfitidea点击:

Naming restrictions of variables in java

java

提问by NPKR

Why are special characters (except $, _) not allowed in Java variable names?

为什么Java 变量名中不允许使用特殊字符(除了$, _)?

回答by assylias

This is not the case - many special characters are actually valid for identifiers. It is defined in the JLS #3.8:

情况并非如此——许多特殊字符实际上对标识符有效。它在 JLS #3.8 中定义:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
[...]
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.

标识符是 Java 字母和 Java 数字的无限长度序列,其中第一个必须是 Java 字母。
[...]
“Java 字母”是该方法Character.isJavaIdentifierStart(int)返回 true的字符。
“Java 字母或数字”是方法Character.isJavaIdentifierPart(int)返回 true的字符。

For example, this is a valid variable name:

例如,这是一个有效的变量名:

String s?èê?á¢é£¥ = "bc";

You can see all the valid characters with this simple code:

您可以使用以下简单代码查看所有有效字符:

public static void main(String args[]) {
    for (int i = 0; i < Character.MAX_VALUE; i++) {
        if (Character.isJavaIdentifierPart(i)) {
            System.out.println("i = " + i + ": " + (char) i);
        }
    }
}

ps: nice examples on @PeterLawrey's blog

ps:@PeterLawrey 博客上的好例子

回答by Peter Lawrey

There is actually an enormous number special characters which are allowed in Java identifiers as it is. For example, you can have every currency symbol, and all 10 continuation characters(not just _)

实际上,Java 标识符中允许使用大量特殊字符。例如,您可以拥有每个货币符号和所有 10 个连续字符(不仅仅是 _)

if( ? ? ? == ? ? ? || ¢ + ¢== ?)

Even more bizarrely you can have characters which are invisible or cause the text to be printed backwards.

更奇怪的是,您可以使用不可见的字符或导致文本向后打印。

The following program has \u202ein its identifiers resulting in it "special" appearance.

以下程序\u202e在其标识符中具有“特殊”外观。

for (char c??h = 0; c??h < Character.MAX_VALUE; c??h++)
    if (Character.isJavaIdentifierPart(c??h) && !Character.isJavaIdentifierStart(c??h))
        System.out.printf("%04x <%s>%n", (int) c??h, "" + c??h);

This prints all the special characters allowed in an identifier which compiles and runs.

这将打印编译和运行的标识符中允许的所有特殊字符。

http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html

http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html

http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html

http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html

回答by david99world

The following code is all valid in Java...

以下代码在 Java 中均有效...

int Δ = 1;
double π = 3.141592;
String 你好 = "hello";
Δ++;
System.out.println(Δ);

I'd say those are pretty special characters for variable names.

我会说这些是变量名称的非常​​特殊的字符。

Source : http://rosettacode.org/wiki/Unicode_variable_names#Java

来源:http: //rosettacode.org/wiki/Unicode_variable_names#Java