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?
Here's another option, indeed an overkill though:
You can do this as well
where \u005c is the Unicode for \
before javac does anything else, it first convert all \u#### to a char. so your code is equivalent to
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.
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:...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
...but
char
is a numeric type, so you could do this:Of course, you could do this:
...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'\''
).