Java default encoding, java default encoding

Source: Internet
Author: User

Java default encoding, java default encoding

Java default encoding

For example, if String. getBytes () is used, the default encoding is called. The process is as follows:

Get the default encoding: String csn = Charset. defaultCharset (). name ();

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

How to obtain the default encoding? The source code is 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 the VM startup, usually depending on the region settings and Character Set of the underlying operating system.

Public static Charset defaultCharset (){
If (defacharcharset = null ){
Synchronized (Charset. class ){
String csn = AccessController. doPrivileged (new GetPropertyAction ("file. encoding"); // encoding set for system attribute file. encoding
Charset cs = lookup (csn );
If (cs! = Null)
DefaultCharset = cs;
Else
DefaultCharset = forName ("UTF-8"); // UTF-8 is used if the encoding set by the system is not available
}
}
Return defaultCharset;
}

 

File. encoding settings(This can be set at runtime ):
Enter java in the command line, and the-D description will appear in the prompt:
-D = # set a system property
-D is followed by a key-value pair to set a system attribute.
For-Dfile. encoding = UTF-8, it is to set the system property file. encoding to UTF-8
It is actually the file encoding.

 

Java default encodingSummary:

First look at the encoding set for file. encoding (the code will be run in file encoding for no garbled Characters During eclipse runtime)

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

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

 

Note:

Generally, our computer systems use the default GBK encoding. If the cmd command is compiled, GBK is used by default.

Compile the Code (the encoding is the same as the file encoding, otherwise the compilation will not pass, I guess it may be to prevent compilation garbled): javac-encoding UTF-8 Test. java

Run the code (that is, defaultCharset, the default code used by the java program) java-Dfile. encoding = UTF-8 Test

There is no need to associate compilation encoding with running encoding. Compile the code (must be consistent with the file to ensure the correct compilation of the file) and execute the code (set the default encoding for the java program (JRE ). This is why the class code in the jar package can be executed regardless of the encoding.

You can also compile and execute the code when using the eclipse hybrid file. The reason is: I personally think that eclipse and other development environments will use the file encoding format by default for compiling, files (files stored in the Program) run the code format (because the main () method is run, there can be multiple main (), but the program entry is one)

When running on the server, it will not be set automatically like eclipse. It is better to configure and execute the Encoding

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.