The difference between Java code Compilation and C + + code compilation

Source: Internet
Author: User

Java compilation principle
1. The Java compilation process is different from the C + + compilation process
The Java compiler compiles the Java source program into the JVM executable code--java byte code.

Java will generally follow the following process during the compilation process:
(1) The JDK determines the source code character set based on the compilation parameter encoding. If you do not specify this parameter, the operating system encoding format is obtained based on the file.encoding parameters of the operating system, and windows in China is usually GBK.
(2) The JDK compiles the source file into the Unicode pattern within Java based on the above character set information, and saves the compiled content to memory.
(3) The JDK writes memory-well-preserved memory information to the. class file, generating the final binary.

Many people will find themselves in the IDE configured with the source file character set to UTF-8 (unlike the operating system default character set), and then run Javac will be an error, this is because the non-encoding parameter of the compilation process will default to use the system's character set. The default is GBK in Windows. When compiling in the IDE, the IDE adds the parameter to the compilation parameters.

If you use ant to compile the activity, then please confirm the character set of your source code and then add the encoding parameter to the corresponding ant compilation task.
This means that if your previous project uses the system default character set (GBK) to edit your source code, then if you switch to UTF-8, that means you need to re-modify your ant script and repackage your product.


C + + compilation process:
When the C compiler compiles code that generates an object, the code is generated for running on a particular hardware platform. Therefore, during the compilation process, the compiler converts all references to symbols into a specific memory offset by looking up the table to ensure that the program runs.

Java compilation process:
The Java compiler does not compile references to variables and methods into numeric references, and does not determine memory layout during program execution, but rather preserves symbolic reference information in bytecode, creates a memory layout by the interpreter during the run, and then determines the address of a method by looking up a table. This effectively guarantees the portability and security of Java.

2. How the JVM works
The task of running the JVM character code is done by the interpreter. Explain the execution of the process in three steps: Code loading, code validation, and code execution.

The work of loading code is done by the class loader ClassLoader. The class loader is responsible for loading all the code required to run a program, which also includes classes inherited by classes in program code and

The class used. When a class loader loads a class, the class is placed in its own namespace. There is no other way between classes that can affect other classes, except by using symbols to refer to classes other than their namespaces. All classes on this computer are in the same address space, and all classes imported from the outside have a separate namespace of their own. This allows local classes to achieve higher operational efficiencies by sharing the same namespace, while ensuring that they do not interact with classes imported from outside.

When all the classes required by the running program are loaded, the interpreter can determine the memory layout of the entire executable program. The interpreter establishes the corresponding relationship and query table for the symbolic reference and the specific address space. By determining the internal layout of the code at this stage, Java solves the problem of the subclass being broken by the superclass, and also prevents the code from being accessed illegally.

The code that is loaded is then checked by the bytecode validator. The validator can find many errors such as overflow of operand stack, illegal data type conversion, etc. After the validation, the code begins to execute.

There are two ways to execute Java bytecode:

1) Instant compilation: The interpreter compiles the bytes into a machine code before executing the machine code.

2) explain how to perform: The interpreter completes all operations of the Java bytecode program by interpreting and executing a small piece of code each time.

The difference between Java code Compilation and C + + code compilation

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.