Reputation: 2652
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?
Upvotes: 13
Views: 18949
Reputation: 1075039
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 '\''
).
Upvotes: 13
Reputation: 45443
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.
Upvotes: 1
Reputation: 2049
You can do this as well
char a = '\u005c\u0027';
where \u005c is the Unicode for \
Upvotes: 9
Reputation: 9260
Here's another option, indeed an overkill though:
char c = "\u0027".charAt(0);
Upvotes: 1