Yesterday, a junior brother who had just learned Java sent me a program and said that he could compile it alive and die, and he always reported coding problems. He tried it myself, but there was a problem...
When we edit a Java source file to save it, it is saved in the default character encoding of the operating system (the default character set of Windows xp is GBK). When calling the javac command, the source file will be encoded and converted in one go. If no character set is specified, it will be converted from the default character set of the operating system to the default unicode character set inside Java, and then the source file will be compiled into a class file and saved to the hard disk in the form of unicode encoding.
After carefully checking the error, I found that it was a file encoding problem. His file was encoded by UTF-8, so the -encoding was added and compiled successfully~~
public class Test { public static void main(String[] args) { System.out.println("Hello!"); }}D:/>javac Test.java
Test.java:3: Warning: System.out.println("日本?); ^Test.java:3: Unfinished string literal value System.out.println("日本?); ^Test.java:3: ';' System.out.println("日本?); ^Test.java:5: The end of the file has been reached when syntax parsing}D:/>javac -encoding utf-8 Test.java
When jdk is compiled, if the encoding format of the java source program is not specified with the -encoding parameter, javac will first obtain the encoding format used by the operating system by default. That is, when compiling the java program, if the encoding format of the source program file is not specified, jdk will first obtain the default encoding format of the operating system, xp is GBK, and then jdk will convert the java source program from this encoding format to the default unicode format within java and put it in memory. Then, javac compiles the converted unicode format file into a .class class file. At this time, the .class file is unicode encoded and it is temporarily placed in memory. Then, jdk saves the compiled class file encoded in unicode to the operating system to form a .class file. The final .class file is a class file with content saved in unicode encoding format. It contains Chinese strings in the source program, but at this time it has been converted into unicode format through the installation system encoding format.
Generally, I prefer to use UTF-8, which can run normally under Windows and Linux...
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.