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 guaranteed to use any particular encoding?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
“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
chartype, 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, castingchartointwill always give you a UTF-16 value if thecharactually contains a character from that charset. If you just poked some random value into thechar, 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 anint, sincechardoesn’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.