java程序中只能用ascll码,为什么某些ASCII字符不能以Java源代码中的'\ uXXXX'形式表示?...

I stumbled over this (again) today:

class Test {

char ok = '\n';

char okAsWell = '\u000B';

char error = '\u000A';

}

It does not compile:

Invalid character constant in line 4.

The compiler seems to insist that I write '\n' instead. I see no reason for this, yet it's very annoying.

Is there a logical explanation why characters that have a special notation (like \t, \n, \r) must be expressed in that form in Java source?

解决方案

Unicode characters are replaced by their value, so your line is replaced by the compiler with:

char error = '

';

which is not a valid Java statement.

This is dictated by the Language Specification:

A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its input, translating the ASCII characters \u followed by four hexadecimal digits to the UTF-16 code unit (§3.1) of the indicated hexadecimal value, and passing all other characters unchanged. Representing supplementary characters requires two consecutive Unicode escapes. This translation step results in a sequence of Unicode input characters.

This can lead to surprising stuff, for example, this is a valid Java program (it contains hidden unicode characters) - courtesy of Peter Lawrey:

public static void main(String[] args) {

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 %n", (int) c‮h, "" + c‮h);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值