In what encoding is a Java char stored in?

后端 未结 3 1311
忘掉有多难
忘掉有多难 2020-12-05 15:40

Is the Java char type guaranteed to be stored in any particular encoding?

Edit: I phrased this question incorrectly. What I meant to ask is are char literals

相关标签:
3条回答
  • 2020-12-05 16:15

    Originally, Java used UCS-2 internally; now it uses UTF-16. The two are virtually identical, except for D800 - DFFF, which are used in UTF-16 as part of the extended representation for larger characters.

    0 讨论(0)
  • 2020-12-05 16:16

    "Stored" where? All Strings in Java are represented in UTF-16. When written to a file, sent across a network, or whatever else, it's sent using whatever character encoding you specify.

    Edit: Specifically for the char type, see the Character docs. Specifically: "The char data type ... are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities." Therefore, casting char to int will always give you a UTF-16 value if the char actually contains a character from that charset. If you just poked some random value into the char, it obviously won't necessarily be a valid UTF-16 character, and likewise if you read the character in using a bad encoding. The docs go on to discuss how the supplementary UTF-16 characters can only be represented by an int, since char doesn't have enough space to hold them, and if you're operating at this level, it might be important to get familiar with those semantics.

    0 讨论(0)
  • 2020-12-05 16:25

    A Java char is conventionally used to hold a Unicode code unit; i.e. a 16 bit unit that is part of a valid UTF-16 sequence. However, there is nothing to prevent an application from putting any 16 bit unsigned value into a char, irrespective of what it actually means.

    So you could say that a Unicode code unit can be represented by a char and a char can represent a Unicode code unit ... but neither of these is necessarily true, in the general case.

    Your question about how a Java char is stored cannot be answered. Simply said, it depends on what you mean by "stored":

    • If you mean "represented in an executing program", then the answer is JVM implementation specific. (The char data type is typically represented as a 16 bit machine integer, though it may or may not be machine word aligned, depending on the specific context.)

    • If you mean "stored in a file" or something like that, then the answer is entirely dependent on how the application chooses to store it.


    Is the Java char type guaranteed to be stored in any particular encoding?

    In the light of what I said above the answer is "No". In an executing application, it is up to the application to decide what a char means / contains. When a char is stored to a file, the application decides how it wants to store it and what on-disk representation it will use.


    FOLLOWUP

    What about char literals? For example, 'c' must have some value that is defined by the language.

    It depends on the character literal form, and what the character is. For instance, 'c' will have the value of the bottom 16 bits of the Unicode codepoint for lowercase 'c'. But a literal expressed as '\uxxxx' may no represent a valid Unicode codepoint. Or (depending on that the application means) it may not represent a character at all.

    This is also (potentially) complicated by the encoding of the source code file. It is theoretically possible to represent your source code in a custom character encoding in which (for the sake of argument) uppercase letters are encoded as lowercase, and vice versa. If you did this, and you were able to register the corresponding Charset encoder and decoder before launching the compiler, then a literal that looks like 'c' (viewing the input as ASCII or UTF-8) would actually have the value 67 in the compiler program rather than 99.

    0 讨论(0)
提交回复
热议问题