Java“中文”编程-java为什么可以使用中文标识符
2016-12-08 23:57
549 查看
Java是国际化的编程语言,印象中看到可以用任何语言做标识符,于是验证一下,然后动手试了一下
编译执行结果
为什么可以java支持中文作为标识符呢?
书上都是这样子写的:
* 标识符由字母、数字、下划线“_”、美元符号“$”组成,第一个字符不能是数字
* 不能把java关键字作为标识符
* 标识符没有长度限制
* 标识符对大小写敏感
但是这样子明显和测试的结果不一致,标识符是中文也可以,不一定是字母,那么我就去翻一下官网java教程说明http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8
3.8. Identifiers
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
Identifier:
IdentifierChars”>IdentifierChars but not a 3.html#jls-Keyword”>Keyword or 3.html#jls-BooleanLiteral”>BooleanLiteral or 3.html#jls-NullLiteral”>NullLiteral
IdentifierChars:
JavaLetter {JavaLetterOrDigit}
JavaLetter:
any Unicode character that is a “Java letter”(//使用的Unicode字符集)
JavaLetterOrDigit:
any Unicode character that is a “Java letter-or-digit”
“Java letter” is a character for which the method Character.isJavaIdentifierStart(int) returns true.
“Java letter-or-digit” is a character for which the method Character.isJavaIdentifierPart(int)
returns true.
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.(//重点在这里)
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
以上简略翻译一下官网文档说明。内容都是简单的,只翻译了重点。 不知道以前的java规定是字母,还是翻译教学有误。我觉得翻译应该是加引号的‘‘Java 字母’’,而不是字母。现在官网的教程说明已经很清楚了,但是我们的教材还没更新,本身学习就具有滞后性,再加上教材的滞后性,这样显得更加滞后了。
那有人也会说,用中文作标识符不好,如知乎的这个问题,那再教你可以用中文作标识符有什么意义呢?
中文本身不太适合做编程,标识符使用英文才能更好地标识。尝试用中文去作标识符的意义在于深刻认识标识符是什么,Unicode是什么,编码是什么,而不是书说是什么就是什么。
class 输出类{ public static void main(String[] args) { String 字符串变量1="你好,JAVA-1\n"; System.out.printf(字符串变量1); 输出方法(); } private static void 输出方法(){ String 字符串变量2="你好,JAVA-2"; System.out.printf(字符串变量2); } }
编译执行结果
为什么可以java支持中文作为标识符呢?
书上都是这样子写的:
* 标识符由字母、数字、下划线“_”、美元符号“$”组成,第一个字符不能是数字
* 不能把java关键字作为标识符
* 标识符没有长度限制
* 标识符对大小写敏感
但是这样子明显和测试的结果不一致,标识符是中文也可以,不一定是字母,那么我就去翻一下官网java教程说明http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8
3.8. Identifiers
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
Identifier:
IdentifierChars”>IdentifierChars but not a 3.html#jls-Keyword”>Keyword or 3.html#jls-BooleanLiteral”>BooleanLiteral or 3.html#jls-NullLiteral”>NullLiteral
IdentifierChars:
JavaLetter {JavaLetterOrDigit}
JavaLetter:
any Unicode character that is a “Java letter”(//使用的Unicode字符集)
JavaLetterOrDigit:
any Unicode character that is a “Java letter-or-digit”
“Java letter” is a character for which the method Character.isJavaIdentifierStart(int) returns true.
“Java letter-or-digit” is a character for which the method Character.isJavaIdentifierPart(int)
returns true.
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.(//重点在这里)
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
以上简略翻译一下官网文档说明。内容都是简单的,只翻译了重点。 不知道以前的java规定是字母,还是翻译教学有误。我觉得翻译应该是加引号的‘‘Java 字母’’,而不是字母。现在官网的教程说明已经很清楚了,但是我们的教材还没更新,本身学习就具有滞后性,再加上教材的滞后性,这样显得更加滞后了。
那有人也会说,用中文作标识符不好,如知乎的这个问题,那再教你可以用中文作标识符有什么意义呢?
中文本身不太适合做编程,标识符使用英文才能更好地标识。尝试用中文去作标识符的意义在于深刻认识标识符是什么,Unicode是什么,编码是什么,而不是书说是什么就是什么。
相关文章推荐
- 第2章 Java编程基础——FAQ2.01 Java中的标识符如何命名?可以用中文吗?
- 使用java.net.URLEncoder.encode()可以对要传递的中文进行编码
- java泛型编程学习 笔记一:为什么要使用泛型
- .Net 、Java可以支持中文标识符
- Java网络编程从入门到精通(12):使用isReachable方法探测主机是否可以连通
- java中的String为什么可以使用==比较字符串内容?
- JAVA Socket编程学习8--为什么使用Netty
- 为什么局部变量必须以final修饰(或者有final实效:java8)才可以在内部类中使用?
- 使用java实现压缩/解压(可以解决中文乱码)
- 为什么 Kotlin 调用 java 时可以使用 Lambda? —— SAM 转换机制的介绍
- (二) java并发编程--为什么使用多线程
- 在Java中使用中文编程
- Java 有几程方法可以实现一个线程?用什么关键字修饰同步?stop()和suspend()为什么不推荐使用?
- java中的String为什么可以使用==比较字符串内容?
- Java网络编程从入门到精通(15):为什么要使用SocketAddress来管理网络地址
- Java 中,为什么在静态方法中可以调用的方法、可以使用的成员变量必须是静态的?
- 为什么Java同包目录下的类不需要引入就可以直接使用
- Eclipse下使用Java Properties文件的中文问题
- Java高级编程:使用打印服务API
- 在J2ME编程中使用Nokia的中文模拟器