I. Java program operation mechanism
High-level computer languages can be classified into compiled and interpreted languages according to the way the program is executed.
Compiled Languages: The source code of a program is written in such a way that it needs to be compiled by a compiler to produce a machine language target file, which is executed directly on the computer. Compiled languages are represented by C, C++, etc.
Interpreted Languages: Source code is interpreted and executed line by line by an interpreter, so there is no need for a compiler to generate the target file. Interpreted languages are represented by Python, Ruby, and so on.
The Java language is a special high-level language, because it has the characteristics of both compiled and interpreted languages.Java program code should be compiled first, in the interpretation of two steps. (Compile first, interpret later)
Writing Java Code: Developers write Java code using the Java language and save it to a .java source file.
Java compiler compiles source code: compiles .java source files into platform-independent bytecode files with .class extension.Java compiler performs lexical analysis, syntactic analysis, semantic analysis, code generation, and other processes to convert the source code into bytecode files.
Java virtual machine load and interpret byte code: Java virtual machine (JVM) is the Java language to achieve the key to cross-platform, its role is to load and interpret the compiled byte code file.JVM will first load the byte code file to generate the corresponding class object, and then interpret the execution of the instructions, and ultimately, the results of the implementation of the output.
II. Java Virtual Machine
Java Virtual Machine (Java Virtual Machine, abbreviated as JVM) is a virtual computer that provides a standard runtime environment to execute Java programs.
A Java virtual machine is a virtual computer that simulates all or part of the functionality of a computer. Like an actual computer, it consists of two parts: the hardware and the operating system. The difference is that the computer hardware and operating system of the JVM are virtual and do not exist in the physical world, but are implemented by software.
The JVM consists of the following three subsystems:
Class Loader Subsystem (Class Loader Subsystem): responsible for loading bytecode files and transforming them into data structures recognizable by the JVM. The class loader loads the bytecode file according to the name or other special attributes and then generates instances of the class.
Runtime data area (Runtime data area): including the method area, heap, virtual machine stack, local method stack and program counter. These make up the memory area required during the runtime of a Java program.
Execution Engine (Execution Engine): responsible for interpreting and executing the byte code file. the JVM provides two execution engines: interpreter and JIT (Just In Time) compiler. The interpreter interprets the byte code instruction by instruction for execution, while the JIT compiler will run the program on the hot code to compile and optimize and make the program run faster.
JVM has cross-platform, memory saving, automatic garbage collection and other features. It is one of the core and key technologies of the Java platform.
III. Java Cross-Platform
Platform: operating system platform (e.g., Windows, Linux, macOs).
Java cross-platform: Java programs can run the same code on any operating system and do not require any changes to the code.
Java cross-platform works on the principle that source code written in Java (.java files) is first compiled into bytecode (.class files), which can then be run on any platform with a JVM. The bytecode can then be run on any platform equipped with the JVM, which uses the bytecode to provide an abstract, operating system-independent environment for running computer programs. Thanks to the Java Virtual Machine, Java programs can be run on a variety of operating systems such as Windows, Linux, Mac, and so on.
Java's cross-platform benefits are a key reason for its widespread use in enterprise and Internet applications.Java's cross-platform nature means that developers can easily create and deploy applications for different operating systems without the need for multiple skill sets.Java's ability to achieve cross-platform is valuable to both developers and users.
summarize
The focus is on understanding the cross-platform principles of the Java language.