Default encoding for Java

Source: Internet
Author: User
Tags locale

Default encoding for Java

For example, String.getbytes () calls the default encoding, the process is as follows:

The default encoding is obtained first: String CSN = Charset.defaultcharset (). Name ();

Use if the default encoding has an exception: "Iso-8859-1"

So how does the default encoding get? Read the following source code as follows

The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the Underlying operating system.

The default character set is determined during virtual machine startup, usually depending on the locale and character set of the underlying operating system.

public static Charset Defaultcharset () {
if (Defaultcharset = = null) {
Synchronized (Charset.class) {
String CSN = accesscontroller.doprivileged (New getpropertyaction ("file.encoding"));//encoding of the System Properties file.encoding settings
Charset cs = lookup (CSN);
if (cs! = null)
Defaultcharset = CS;
Else
Defaultcharset = forname ("UTF-8");//The encoding of the system settings is used UTF-8
}
}
return defaultcharset;
}

file.encoding Settings (can be set at runtime):
Enter Java on the command line, and the-D description appears in the hints given:
-d= # Set a system property
-D needs to be followed by a key-value pair to set a system property
For-dfile.encoding=utf-8, setting the system property file.encoding to UTF-8
It's actually the file code.

Java Default Coding Comprehension Summary :

First look at the encoding set by file.encoding (Eclipse Runtime, in order to not garbled will be run as a file encoding)

If no encoding is set, use "UTF-8"

If the encoding you set is not available, use "iso-8859-1"

Note the point:

Our computer system is generally the default GBK encoding, if the cmd command is compiled by default with GBK.

Compile the code (compile with encoding and file encoding consistent, otherwise it will compile does not pass, I guess may be to prevent compilation garbled): Javac-encoding UTF-8 Test.java

Run the encoding (that is, the default encoding used by the Defaultcharset,java program) Java-dfile.encoding=utf-8 Test

There is no necessary connection between the compiled code and the Run code. Compile the encoding (must be consistent with the file, ensure the correct compilation of the file), and execute the encoding (default encoding for the Java Program (JRE)). This is why the class code in the jar package can be executed regardless of the encoding.

can also be compiled when using the Eclipse Hybrid file encoding, because: personally think that the development environment such as Eclipse will be compiled by default file encoding format, file (file in the program) encoding format run (because the main () method is running, main () can be multiple, but the program entrance is one)

When running on a server, it will not be set up automatically like Eclipse, it is best to configure code execution

Default encoding for Java

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.