Java Unicode Confusion

前端 未结 5 1050
无人及你
无人及你 2020-12-18 05:03

HEy all, I have only just started attempting to learn Java and have run into something that is really confusing!

I was typing out an example from the book I am using

5条回答
  •  感情败类
    2020-12-18 05:59

    One thing great about Java is that it is unicode based. That means, you can use characters from writing systems that are not english alphabets (e.g. Chinese or math symbols), not just in data strings, but in function and variable names too.

    Here's a example code using unicode characters in class names and variable names.

    class 方 {
        String 北 = "north";
        double π = 3.14159;
    }
    
    class UnicodeTest {
        public static void main(String[] arg) {
            方 x1 = new 方();
            System.out.println( x1.北 );
            System.out.println( x1.π );
        }
    }
    

    Java was created around the time when the Unicode standard had values defined for a much smaller set of characters. Back then it was felt that 16-bits would be more than enough to encode all the characters that would ever be needed. With that in mind Java was designed to use UTF-16. In fact, the char data type was originally used to be able to represent a 16-bit Unicode code point.

    The UTF-8 charset is specified by RFC 2279;

    The UTF-16 charsets are specified by RFC 2781

    The UTF-16 charsets use sixteen-bit quantities and are therefore sensitive to byte order. In these encodings the byte order of a stream may be indicated by an initial byte-order mark represented by the Unicode character '\uFEFF'. Byte-order marks are handled as follows:

    When decoding, the UTF-16BE and UTF-16LE charsets ignore byte-order marks; when encoding, they do not write byte-order marks.
    
    When decoding, the UTF-16 charset interprets a byte-order mark to indicate the byte order of the stream but defaults to big-endian if there is no byte-order mark; when encoding, it uses big-endian byte order and writes a big-endian byte-order mark.
    

    Also see this

提交回复
热议问题