1-2. Java environment setup

Yuque original text link

1. JDK installation

image.png

  • Just download and install it. The jdk/bin directory after installation is as follows

image.png
image.png

  • Set the bin path to the system environment variable after installation. The path here is F:\Java\jdk1.8.0_311\bin (so that javac and java can be accessed in any directory)

image.png

  • Verify that environment variables are configured correctly
    • java -version: View java version
C:\Users\wangchun>java -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)

2、Hello World

2-1. Hello World example

  • Strict case distinction in Java
  • Hello.java
/**
 * 文档注释(可以通过Javadoc命令生成文档)
 * public 公共的
 * class 类
 * Hello 类名,这个必须和文件名一致
 * {} 类的作用域
 */
public class Hello {

    // 单行注释
    // static 静态的
    // void 无返回值
    // main 主函数,程序的入口方法
    public static void main(String[] args){

        /*
         * 多行注释
         * 在控制台输出Hello World!
         */
        System.out.println("Hello World!");
    }
}
  • javac Hello.java: Automatically generate Hello.class bytecode file after execution
  • java Hello: JVM runs bytecode file
C:\Users\wangchun\Desktop>javac Hello.java

C:\Users\wangchun\Desktop>java Hello
Hello World!

2-2. Class name and file name

  • There can be multiple classes in a source file, but only one class can be modified by public
  • When executing the following code, an error will be reported

public class Hello {

    public static void main(String[] args){

        System.out.println("Hello World!");
    }
}

public class Hello1 {

    public static void main(String[] args){

        System.out.println("Hello World!");
    }
}
  • If the above code is executed, an error will be reported. Just remove the public of Hello1.
F:\>javac Hello.java
Hello.java:24: 错误: 类Hello1是公共的, 应在名为 Hello1.java 的文件中声明
public class Hello1 {
       ^
1 个错误
  • After javac will generate two class files Hello.class Hello1.class

2-3. Comments

  • Comments will not participate in compilation, and there will be no comment content in the bytecode file generated after compilation.
  • Single line comment //
  • Multi-line comments /* */
  • Documentation comments /** */ can be extracted through the javadoc command
  • Documentation comment code example
/**
 文档注释
 */
public class Hello {

	/**
	 main方法
	 @param args 数组
	 */
    public static void main(String[] args){

		// 单行注释
        System.out.println("Hello World!");
    }
}

2-4、javadoc

  • javadoc generates documentation
F:\>javadoc -d newName Hello.java
正在加载源文件Hello.java...
正在构造 Javadoc 信息...
标准 Doclet 版本 1.8.0_311
正在构建所有程序包和类的树...
正在生成newName\Hello.html...
正在生成newName\package-frame.html...
正在生成newName\package-summary.html...
正在生成newName\package-tree.html...
正在生成newName\constant-values.html...
正在构建所有程序包和类的索引...
正在生成newName\overview-tree.html...
正在生成newName\index-all.html...
正在生成newName\deprecated-list.html...
正在构建所有类的索引...
正在生成newName\allclasses-frame.html...
正在生成newName\allclasses-noframe.html...
正在生成newName\index.html...
正在生成newName\help-doc.html...

image.png
image.png

3. Environment variables

3-1. Path function

  • window cmd command line: path (view the currently configured path attribute)
C:\Users\wangchun>path
PATH=C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Windows\System32\OpenSSH\;
F:\Java\jdk1.8.0_311\bin;
C:\Users\wangchun\AppData\Local\Microsoft\WindowsApps;
  • Enter the command notepad in the D drive directory and try to open Notepad. Try to find whether there is a Notepad program in the D drive directory. If there is a Notepad program, open Notepad. If not, go to the path saved in the path to find it. If there is, open Notepad. Failure if not
  • Normally, the program can only be run by running this command in the path where xxx.exe is located; but by adding the path to Path, the program can be run in any path. Path stores environment variables, which are guaranteed to work in any directory. can run the program
C:\Users\wangchun>notepad

C:\Users\wangchun>aa
'aa' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
  • Set temporary path (valid only when the CMD command line is opened, and will be invalid after closing)
    • set path=xx
    • set path=xx;%path%
C:\Users\wangchun>set path=aa

C:\Users\wangchun>path
PATH=aa


C:\Users\wangchun>set path=bb;%path%

C:\Users\wangchun>path
PATH=bb;aa

3-2、classpath

  • There is an environment variable classpath in the system, the default is the current directory
  • Function: After setting this classpath path, when executing the bytecode file, it will go to this path to find the corresponding bytecode file.
  • set classpath is only a temporary modification, this setting will disappear when the current cmd window is closed!
  • But if you use start to open a new window in the current cmd, the set path will still be valid in the new cmd window.
  • set classpath
    • Check the current classpath value
  • set classpath=d:\
    • Set the classpath attribute and search for the corresponding class file in the classpath directory (d:\path). If not, an error message will be reported.
  • set classpath=d:;
    • Set the classpath attribute and search for the corresponding class file in the classpath directory (d:\path). If not found, search in the directory where the Hello.java file is located.
  • No semicolon after the path: During execution, only the folders under the classpath will be searched. If not found, an error will be reported.
  • Add a semicolon after the path: first check whether it is in the classpath path. If not, check whether it is in the current folder. If it still does not exist, an error will be reported.
C:\Users\wangchun\Desktop>set classpath
classpath=d:\

C:\Users\wangchun\Desktop>set classpath=d:\

C:\Users\wangchun\Desktop>set classpath
classpath=d:\

C:\Users\wangchun\Desktop>java Hello
错误: 找不到或无法加载主类 Hello

C:\Users\wangchun\Desktop>set classpath=d:\;

C:\Users\wangchun\Desktop>set classpath
classpath=d:\;

C:\Users\wangchun\Desktop>java Hello
Hello World!

3-3、JAVA_HOME

  • Environment variable JAVA_HOME F:\Java\jdk1.8.0_311\bin

image.png

  • The previous PATH path can be configured like this: %JAVA_HOME%\bin

4. Java composition

  • JRE (Java Runtime Environment): Java runtime environment
    • Including core class libraries required by JVM and Java programs, etc.
    • To run a developed Java program, the computer only needs to install JRE
  • JDK (Java Development Kits): Java development toolset
    • JDK is provided to Java developers, including Java development tools and JRE.
    • Once JDK is installed, there is no need to install JRE separately.
    • The development tools include: compilation tool javac.exe, packaging tool jar.exe

image.png image.png

5. Cross-platform principle

5-1. Java cross-platform principle

  • JVM (Java Virtual Machine): Because of JVM, Java programs can be executed on three operating systems. This is the cross-platform nature of Java programs, which makes Java have good portability.
  • JVM is not cross-platform. Windows has a JVM corresponding to Windows, and Linux has a JVM corresponding to Linux.
  • Process: Source file (.java) -> (Compiler javac.exe) -> Bytecode file (.class) -> (JVM executes java.exe) -> Operating system (compile once, run anywhere )

image.png

5-2. Cross-platform principle of C language

image.png

Guess you like

Origin blog.csdn.net/sinat_35615296/article/details/134901568