Analysis of Java character encoding principles

Source: Internet
Author: User

 

I encountered a Java garbled problem this week, so I made some experiments and knowledge about Java coding. A simple analysis is as follows:

Let's take a look at the following code:

Import


Java. Io. unsupportedencodingexception;

Public



Class


Charsettest {


Public



Static



Void


Main (string [] ARGs)
Throws


Unsupportedencodingexception {

String test =
"


Basketball


"
;


Byte

[]
Defaultresult = test. getbytes ();


For


(
Byte


E: defaultresult ){

System.
Out

. Print (E
+
""
);

}

System.
Out

. Println (system.
Getproperty

(
"File. encoding"
));

System.
Out

. Println (
"Test ="

+ Test );

}

}

1. Run javac charsettest. Java to compile properly, but the following warning is returned:

Charsettest. Java: 5: Warning: unmappable character
For encoding ASCII
String test = "???? ";

Why is that? For the Java compiler, charsettest. Java is a text file, which must be parsed by the Java compiler.
This text file is compiled and generated. Class file. The reason is probably as follows: charsettest. Java must be stored in a certain encoding format.
The interpreter must know what encoding is used for the text file. If it is not specified, the default encoding format of the file is"
ANSI_X3.4-1968 "(different environments may be different), so it will find that the Chinese cannot be interpreted and garbled.
The above problem should be solved by telling the compiler at compilation that the encoding format of the Java file to be compiled is required. Otherwise, the compiler may handle unintelligible characters as garbled characters. Because
Charsettest. Java is in GBK format. Therefore, use the following command:
Javac charsettest. Java-encoding = GBK.

2. Run javac charsettest. Java-encoding = GBK to get the correct class file, but run
Java charsettest:


63 ANSI_X3.4-1968
Test = ??

Now that the compilation is correct, why is the output result garbled? We have already confirmed that the Chinese character strings in the. Class file are correct, and the reason must be in JVM.
When reading this string byte stream from the. Class file and constructing the string object, the incorrect character encoding is used to construct the byte stream. In this way, the bytes from the JVM output string flow to our control.
Garbled characters appear. Obviously, we must tell the JVM the encoding in our console, or what character encoding we want it to use to build the byte stream. If the JVM is not told, compile the file
The format is "ANSI_X3.4-1968" (different environments may be different ).
Assume that we use GBK encoding in the console. If we tell it correctly, it will return the byte stream correctly. The reason is simple. We do not correctly tell JVM that we need it to construct a string.
The encoding format used for the output stream. So what should we do? Run the following command:
Java-dfile. Encoding = GBK charsettest
Expected result:
-64-70-57-14 GBK
Test = Basketball
The above test will not be implemented in eclipse, because eclipse will help us make some judgments. At the same time, different environments may not
Same.


Conclusion:
Java class files use UTF-8 and UTF-16 in JVM. During the entire process, you can see the following code conversion:

 

 

 

 

From the perspective of understanding that no matter which format the source file is used, the compiler will get the correct result as long as the compiler is correctly told. At the same time, as long as the JVM is notified of the correct encoding format required for the output stream, the JVM will always return
Return the output stream in the correct encoding format.

There are two steps to avoid garbled characters:
1. Tell the compiler about your source file encoding.
2. Tell JVM the encoding you want to display or construct the string output stream.
In particular, when JSP code is garbled, pay attention to whether the encoding used in the request is consistent with that used in the Request Parsing. The response encoding and HTML
Whether the charset encoding is consistent.

At the same time, we often encounter JSP, database and other garbled issues. You can find out if there are two reasons:
1. Misunderstanding: a file is GBK encoding, but you think it is UTF-8-type encoding, so use UTF-8 to understand it, there will be garbled.
2.
Powerless type: A file when GBK encoding, you also know that it is GBK encoding, but you want to convert it into a ISO-8859-1 to display, but GBK contains many characters when iso-
If 8859-1 cannot be explained, garbled characters may also appear.

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.