Write "Hello World" in Notepad

1. Write the code to the notebook, naming it the same as the class name, with a suffix of .java.

insert image description here

example:H.java

class H{
    public static void main(String[] args){
         System.out.println("Hello world");
    }
}

2. "Window+R", enter "cmd", click 'OK'; enter the folder where the note is located, E:\Desktop>enter the command and press javac H.javaEnter (to compile), enter the command java Hto run after the compilation is completed.

insert image description here
insert image description here
insert image description here

3. Chinese garbled code solution

insert image description here

class H{
    public static void main(String[] args){
         System.out.println("Hello world   你好 世界");
    }
}

The following garbled characters appear:
insert image description here
If there are Chinese garbled characters when running

1. 记事本另存文件编码格式为ANSI
2. 如果使用utf-8, java -Dfile.encoding=utf-8 H.java 直接运行解决乱码(java17 可以不用编译直接运行源程序)
3. 如果使用utf-8, javac H.java -encoding utf-8 ,再运行 java H就没有乱码

(1) The encoding format of the file saved in Notepad is ANSI, as shown in the figure below:
insert image description here
(2) Use utf-8, java -Dfile.encoding=utf-8 H.java to run directly to solve the garbled characters (java17 can run the source program directly without compiling) (3)
insert image description here
If you use utf-8, javac H.java -encoding utf-8, and then run java H, there will be no garbled characters
insert image description here

Guess you like

Origin blog.csdn.net/qq_56015865/article/details/128919385