[Java Learning Record Part II]


Note: The following operations and commands are only based on Windows system

1. The operating principle of Java programs

  • How does a Java program run?
    First of all, the execution of a Java program needs to go through two steps of compiling and running, corresponding to the two commands javac and java respectively . The javac command is to compile the Java source file (that is, the .java file) to generate the corresponding bytecode file (that is, the .class file), and the java command is to run the .class file. During this process, it should be noted that:
    (1) The .class file generated by compilation is placed in the current folder by default, of course, the storage location can also be specified. Take HelloWorld.java as an example:

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

    Enter the following compile command in a terminal window:

    javac -d . HelloWorld.java	//或者为:javac HelloWorld.java
    

    Among them, -d destdir (in this example. represents the current folder), the common format of the compilation command is:

    javac -d destdir srcFile
    

    Among them, destdir is the storage location of the .class file you want to specify, as long as it is a valid path on the disk; srcFile is the location of the Java source file, which can be an absolute path or a relative path, and the default is under the current path . It should be noted that both paths need to be enclosed in English double quotation marks .
    (2) The .class file generated by compilation has a default file name, that is, the class name defined in the source file is always used as the main file name. When multiple classes are defined in a source file, multiple .class files will be compiled and generated. For example, if a class B is defined in class A, two .class files will be generated: A.class and A$B.class .
    (3) After the Java source file is compiled, it can wait to be called or run as the main program. If it is run as the main program (that is, contains the main() method), you can enter the path where the main program is located in the command line window, and then enter the run command:

    java HelloWorld
    
    • It is worth noting that the main class name can be directly followed by the java command.
    • If the main program needs to call other classes or refer to third-party jar packages, a proper way is to specify the location of the jar package (that is, temporarily specify the search path of CLASSPATH), and put other classes that need to be called together with the main program. In the same folder, take running Flips as an example, it needs to call other classes, and needs to refer to third-party jar packages and specify input parameters. The specific commands are as follows:
    java -classpath ".;E:\zp\study\algs4.jar" Flips 1000000 
    

    Above, -classpath is the parameter option of the running command, which can be abbreviated as -cp. The search paths are separated by ; and placed in English double quotation marks . Flips is the class containing the main() method, and 1000000 is in the Flips class The main() method requires input parameters.

2. Program entry and input and output

2.1 The entry of the Java program - main() method

The general definition of the main() method is:

public static void main(String args[]){
    
    ...}
  • In order to allow the JVM to call freely, the access authority of the main() method is set to public (public access authority).
  • static indicates that the main() method belongs to the main class, and when the JVM calls main() , it can be called directly through this class without creating another object.
  • Since the return value of the main() method will be returned to the JVM, it doesn't make sense, so void indicates that the main() method has no return value.
  • The formal parameter args of the main() method is a String array, and the value of args is passed by the JVM. Its source can be the command line input (or the standard input stream). As mentioned above, when running the Flips class, it is followed by the parameter 1000000. Of course, the actual incoming parameters can also come from redirection and pipelines, such as:
  • Redirect:
java -classpath ".;E:\zp\study\algs4.jar" Flips < data.txt  

This command will read the actual parameters required by the main() method in Flips from the file data.txt.

  • pipeline:
java Data | java -classpath ".;E:\zp\study\algs4.jar" Flips

Among them, the operation java Datawill generate the actual parameters required by the main() method in Flips .

2.2 Input and output

The figure below shows the overall structure of a Java program:
The overall structure of the Java program(1) Compared with the terminal window, its input can come from standard input streams, files, and command line parameters; output can be standard output streams, files, and standard drawing. Pipelines and redirection have been mentioned before, so I won't repeat them here.
(2) Java provides a powerful input and output library, and you can view the corresponding API by yourself.

————to be continued

Guess you like

Origin blog.csdn.net/zp_stu/article/details/126170877