Java - 将 unicode 撇号分配给 char

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13693312/
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 13:39:07  来源:igfitidea点击:

Java - Assign unicode apostrophe to char

javaunicode

提问by priomsrb

I want to assign the value of aphostrophe to a char:

我想将 aphostrophe 的值分配给一个字符:

char a = '\'';

However I would like to use the unicode version of apostrophe (\u0027) to keep it consistent with my code:

但是,我想使用单引号 (\u0027) 的 unicode 版本来使其与我的代码保持一致:

char a = '\u0027';

But doing it this way gives an error saying "unclosed character literal".

但是这样做会产生一个错误,说“未封闭的字符文字”。

How can I do this assignment while still having the unicode code in the code?

我怎样才能在代码中仍然有 unicode 代码的情况下完成这个分配?

采纳答案by T.J. Crowder

The reason \u0027doesn't work is that the unicode escape is handled very earlyby the compiler, and of course, it ends up being ' — which terminates the literal. The compiler actually sees this:

原因\u0027不起作用是编译器很早就处理了 unicode 转义,当然,它最终' 会终止文字。编译器实际上看到的是:

char a = ''';

...which naturally is a problem. The JLS talks about this in relation to line feeds and such in §3.10.4(Character Literals).

……这自然是个问题。JLS 在§3.10.4字符文字)中讨论了与换行等相关的问题

Frankly, I think you're best off writing

坦率地说,我认为你最好写

char a = '\'';

...but charis a numeric type, so you could do this:

...但是char是数字类型,所以你可以这样做:

char a = 0x0027;

Of course, you coulddo this:

当然,你可以这样做:

char a = "\u0027".charAt(0);

...but I think we can all agree that's a bit overkill. ;-)

...但我认为我们都同意这有点矫枉过正。;-)

Oooh, or check out Greg's answer: char a = '\u005c\u0027';(\u005cis, of course, a backslash — so the compiler sees '\'').

哦,还是看看Greg的答案char a = '\u005c\u0027';\u005c是的,当然,一个反斜杠-所以编译器看到的'\'')。

回答by Greg

You can do this as well

你也可以这样做

char a = '\u005c\u0027';

where \u005cis the Unicode for \

其中\u005c\u005c的 Unicode

回答by irreputable

before javac does anything else, it first convert all \u#### to a char. so your code is equivalent to

在 javac 执行其他任何操作之前,它首先将所有 \u#### 转换为字符。所以你的代码相当于

char a = ''';

that's why it doesn't compile.

这就是它不能编译的原因。

\u#### is not just for char/string literals, you can use it anywhere, e.g. in variable names.

\u#### 不仅用于字符/字符串文字,您还可以在任何地方使用它,例如在变量名中。

however, people rarely use non latin chars in identifiers; if someone does, he'll probably use his native charset, and he won't need \u#### either.

然而,人们很少在标识符中使用非拉丁字符;如果有人这样做,他可能会使用他的本地字符集,而且他也不需要 \u####。

therefore we never really see \u#### anywhere other than in char/string literals, this gives the wrong impression to the unsuspected.

因此,除了字符/字符串文字之外,我们从未真正在任何地方看到 \u####,这会给不知情的人带来错误的印象。

if there's time machine, we should probably kill this feature, since it's confusing and it's not used.

如果有时间机器,我们可能应该取消这个功能,因为它很混乱而且没有使用。

回答by nullpotent

Here's another option, indeed an overkill though:

这是另一种选择,但确实有点矫枉过正:

char c = "\u0027".charAt(0);