Java - Assign unicode apostrophe to char

2020-03-01 08:09发布

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

char a = '\'';

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

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?

标签: java unicode
4条回答
We Are One
2楼-- · 2020-03-01 08:52

Here's another option, indeed an overkill though:

char c = "\u0027".charAt(0);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-03-01 09:01

You can do this as well

char a = '\u005c\u0027';

where \u005c is the Unicode for \

查看更多
我只想做你的唯一
4楼-- · 2020-03-01 09:02

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

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.

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.

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

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

查看更多
爷的心禁止访问
5楼-- · 2020-03-01 09:07

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

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).

Frankly, I think you're best off writing

char a = '\'';

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

char a = 0x0027;

Of course, you could do 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'; (\u005c is, of course, a backslash — so the compiler sees '\'').

查看更多
登录 后发表回答