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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With