Java Character Set

Source: Internet
Author: User

Background
Character sets and encodings are two different concepts
Character Set---refers to the number of text in some languages, which is the original encoding of the character set
Encoding---A character set typically has a number of specific codes for computing, storing, transmitting

Note: Some character sets do not have any other specific encoding, they are calculated, stored, transmitted directly using the original encoding.

For example, the Unicode character set, which assigns all characters in the world (including letters, glyphs) uniformly to a unique number, which is the Unicode original encoding
UTF-8 UTF-16 UTF-32 and so on. These encodings are encoded for the Unicode character set
Why not use the character set (in this case, Unicode) to store, transmit, and compute the original encoding directly?
Because Unicode raw encoding requires a minimum of 2 bytes to represent one character
Results in calculations that are incompatible (such as the C string function, which is the end of a byte with a value of 0, and a lot of Unicode characters are interpreted as null), will cause the old program error, of course, and the old program interoperability, incompatible ASCII code will cause problems
Causes storage incompatibility Because the computer CPU has a size-to-end problem, the Unicode source code is 2 bytes in order and interpreted differently on different computers, resulting in parsing errors
Reduce storage space, provide transfer efficiency, because ASCII code in Unicode is at least 2 bytes of storage, the English storage is one times larger, and UTF-8 (fully compatible with the ASCII code, a byte to represent the ASCII code) there is no such problem


Note: The character sets mentioned below actually refer to a specific encoding of a certain character set, which is usually referred to as a specific encoding.

Java Character Set

Java source code file character set
The character set used by the class file (compiled file)
Character set used in in-memory characters
External data has its own character set, and Java interaction data needs to be converted
JSP file Character Set
The character set specified in the JSP file

JAVA strings Use the UCS2 character set in memory, with a character typically 2 bytes and 4 bytes;
In the reading, writing files, network collection, package and other external interaction data, only need to convert the in-memory string and the peripheral byte array to each other, this involves the character set problem, it is best to specify the character set encoding serialization/deserialization,
Otherwise, the JVM uses the character set used by the current system to serialize/deserialize, and if the current system character set is inconsistent with the other, it will cause garbled characters;
For example byte[] dest = str.getbytes ("UTF-8"), if the call this getBytes function to serialize, do not specify a character set, is the system character set gb2312, if the other request with UTF-8, then there is garbled;
Similarly str new String (dest, "UTF-8"), if this function is called to decode, if you do not specify a character set, also use the system character set, may lead to decoding into garbled;


Programmatic impact


When programming, the following points should be noted

1. read File/write file character set

Look at the following code
File File = new file ("/root/tmp"); Does not involve peripherals, but constructs a memory object
InputStream ins = new FileInputStream (file); Open the file stream, this involves peripherals, but the stream is only a byte array, and does not involve encoding
InputStreamReader INSR = new InputStreamReader (INS, "UTF-8");//character reader, this involves character encoding, because to convert from byte array to memory character

Writing files and this similar, mainly involves the conversion of byte char to each other when it comes to character set conversion;
The true stream in Java does not involve a character set, just a byte stream reading and writing


2. Network receive/Contract character set
Received is a byte array, the final send is also a byte array, the conversion time to note the character set on the line
Tomcat uses ISO-8859-1 encoding by default and cannot support Chinese.

3.java source file Character Set
This should explicitly indicate the encoding type of the Java source file when the compiler is working, or it may cause compilation to fail or may cause a run error.
The encoding of the. java file is identical to the encoding specified by Javac, and after the compilation succeeds, it is the UTF-8 character set, and there is no error.

4.JSP file Character Set
The character set of the JSP file itself and the specified character set in the JSP file should be consistent, otherwise it may lead to garbled, compile time lead to error, generally these 2 character sets consistent, OK

Java Character Set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.