为什么我不能在Java中将char绑定到Unicode城堡字符?

2022-09-03 14:38:23
class A {
    public static void main(String[] args) {
        char a = '∀';
        System.out.println(a);
        char castle = '						

答案 1

I suspect that the castle character does not fit in a single char, but rather, requires an int code point. In that case, you could use it in a String literal, but not as a char.

The Javadoc for Character states:

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value.

So my guess would be that that character requires more than 16 bits, so it would need to be treated as an int code point.


答案 2

If your source code file contains non-ascii characters (as does this file), you need to ensure that javac reads it with the proper encoding, otherwise it will default to an encoding that is possibly not the one in which it was saved.

So, if you saved your file in UTF-8 from your editor, you can compile it using:

javac -encoding utf8 A.java

Note that you can also use the unicode codepoint instead of the actual character, this makes the code compilable without the -encoding directive:

char a = '\u2200';              // The codepoint for the ∀ character
String castle = "\ud834\udf47"; // Being a high-surrogate char, the castle cannot fit a single 16 bit char