Principles of Java cross-platform

Source: Internet
Author: User

Java cross-platform is implemented through the Java Virtual Machine (JVM.
Java source file compilation process
The Java application development cycle includes the compilation, download, interpretation, and execution. The Java compiler translates the Java source program into JVM executable code-bytecode. This compilation process is somewhat different from that of C/C ++. When the C compiler compiles an object code, the code is generated to run on a specific hardware platform. Therefore, during the compilation process, the compiler converts all references to the symbol into a specific memory offset through the lookup table to ensure that the program runs properly. The Java compiler does not compile references to variables and methods into numerical references, nor determine the memory layout during program execution. Instead, it retains these symbolic references in bytecode, the interpreter creates a memory layout during running, and then uses the table to determine the address of a method. This effectively ensures the portability and security of Java.
Java interpreter Execution Process
The interpreter runs the JVM bytecode. The execution process is divided into three steps: Code loading, code verification, and code execution. Code loading is completed by the class loader. The class loader is responsible for loading all the code required to run a program. This also includes the classes inherited by the classes in the program code and the classes called by the class. When the class loader loads a class, the class is placed in its own namespace. In addition to referencing your own namespace through symbols, there is no way between classes to affect other classes. All classes on this computer are in the same address space, and all classes introduced from the outside have their own namespace. This allows local classes to achieve high running efficiency by sharing the same namespace, while ensuring that they do not affect each other with the classes introduced from outside. After all the classes required to run the program are loaded, the interpreter can determine the memory layout of the entire executable program. The interpreter establishes a correspondence and query table with a specific address space for symbolic reference. By determining the memory layout of the code at this stage, Java solves the problem of sub-class crash caused by super class changes, and also prevents unauthorized access to the address by the Code.
Then, the loaded code is checked by the bytecode validator. The validator can detect multiple errors such as overflow of the operand stack and illegal data type conversion. After the verification is passed, the code is executed.
Two Java bytecode execution methods
1. Instant compilation method: the interpreter first compiles the bytecode into a machine code and then executes the machine code.
2. Explain execution method: the interpreter interprets and executes a small piece of code each time to complete all operations of Java bytecode.
The second method is usually used. The JVM Specification Description is flexible enough, which makes it highly efficient to translate bytecode into machine code. For applications with high running speed requirements, the interpreter can instantly compile Java bytecode into machine code, thus ensuring the portability and high performance of Java code.
JVM Specification Description
JVM is designed to provide a computer model based on abstract specification descriptions, which provides great flexibility for interpreting program developers, it also ensures that Java code can run on any system that complies with this specification. JVM defines some aspects of its implementation, especially the Java executable code, that is, the Bytecode format. This specification includes the syntax and value of the operation code and operand, the numerical expression of the identifier, and the Java object in the Java class file, and the storage image of the constant buffer pool in the JVM. These definitions provide JVM interpreter developers with the required information and development environment. Java designers hope to give developers the freedom to use Java as they wish.
JVM is a type description defined for Java bytecode independent of a specific platform, and is the basis for Java platform independence.
Comparison and Analysis of Java program execution and C/C ++ Program Execution
If you think of a Java source program as our C ++ source program, the byte code generated after the Java source program compilation is equivalent to the 80x86 machine code (Binary program file) after the C ++ source program compilation ), the JVM virtual machine is equivalent to the 80x86 computer system, and the Java interpreter is equivalent to 80x86CPU. The machine code is run on 80 x86cpu, And the Java bytecode is run on the Java interpreter.
The Java interpreter is equivalent to the "CPU" that runs the Java bytecode, but the "CPU" is not implemented by hardware, but by software. The Java interpreter is actually an application on a specific platform. As long as the interpreter program is implemented on a specific platform, Java bytecode can be run on the platform through the interpreter program, which is the basis of Java cross-platform. Currently, not all platforms have corresponding Java interpreter programs. This is why Java cannot run on all platforms, it can only run on a platform that has implemented a Java interpreter program.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.