Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Assign unicode apostrophe to char

Tags:

java

unicode

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?

like image 563
priomsrb Avatar asked Dec 03 '12 23:12

priomsrb


1 Answers

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.

like image 81
irreputable Avatar answered Sep 18 '22 18:09

irreputable