Talking about C # vs Java (1)

zhaozj2021-02-08  278

CLR VS JVM

Concept

Microsoft has been claiming that the CLR (public language operating environment) is called virtual machines and not JVM virtual

Concept of the machine. This is because the CLR will support all programming languages ​​that follow CTS (public language rules) on it and do not interfere with each other, saying that CLR is more like a platform compared to JVM.

2. Compile

Java compiles the source code into a .class file, run through the Java command. E.g:

Java Test.class

And C # compiles the source code into .exe file. But this EXE file is different from the traditional EXE file, which consists of a list and MSIL (Microsoft Intermediate Language) code. The format is approximately as follows:

PE (Win32-Portable Executable) Head --- EXE File

Inventory ---- The type and class included in the file

Jump command --- jump to the MSIL interpreter

MSIL code

From the above structure, C # can be easily compiled as Java. Different, Microsoft provides tool ILDASM for reading MSIL after compiling.

In addition, although Microsoft puts a variety of benefits to this structure, if Msil looks like a Java compiled code (ie .class file in the code), the C # file structure is more like a Java run command and The .CLASS file is encapsulated by the EXE file. As follows:

C # java pe header - Checklist About this section will discuss the jump command java command msil code .class file

The results of C # compilation are analyzed, and the C # does not have a Window platform because it is compiled into an executable file. In theory, if there is a Linux version of the MSIL interpreter (actually it is part of the CLR) and a Linux version of the C # compiler, you can recompile the C # code written under Window. So the so-called "compiled, run everywhere". In this project, the CTS (public language rules) that maintains code compilation consistency, it is no wonder that free software organizations have proposed to use Microsoft's CLR & CTS architecture to unify the current free software industry compilation environment.

3. Run

The early days of Java is an explanatory language, which later uses Just-in-Time technology in order to improve efficiency, and some tools can generate Java to generate a specific CPU binary code.

C # is also indispensable to select MSIL and JIT mechanisms. According to different situations, it can be divided into 3 categories:

a) Install-time code generation: completely compile the current code into a specific CPU binary code. The reason is called the installation code generation because this compilation process is in installation.

b) JIT: Similar to JAVA's JIT mode.

c) Econojit (Economic JIT): Different from JIT, it is a handheld device that is suitable for a small amount of memory by discarding the generated, compiled code recycled memory.

4. Deploy

Java uses methods corresponding to the package path and directory to generate a layer of directory. Then launch the way using JAR file packages.

C # uses a method of metadata records. The so-called metadata is "List" mentioned in the C # compile results structure above. C # passed the information of the metadata record type and class, compared to the Java directory structure and JAR file mode, encapsulation improvement

A lot. However, there is a benefit of a directory structure is obvious, that is, when some code needs to be modified, just recompile the code that needs to be modified, and override the corresponding class.

Everything is an object

C # and Java claim that everything is an object, but I am afraid only Smalltalk in this statement.

This is mainly because it is difficult to accept the decrease in code performance caused by the basic data type into objects. So C # and Java have proposed solutions to this issue. Java retains the basic data type and provides a corresponding class for each type, such as the INT type corresponding to the INTEGER class, the LONG type corresponds to the long class. When you need to use a class to describe the basic data type, you can generate the corresponding class, as follows:

INT n = 5;

Integer in = new integer (n);

After use, the value of the corresponding class can be assured to the basic data type, as follows:

Integer in = new integer (5);

INT n = in.intValue ();

Compared with corresponding, C # also uses the way to provide a corresponding class for the basic data type, but uses unboxing and boxing operations. The so-called packing is to convert the basic data type to the corresponding class. The role of the out of the box is just the opposite.

When you try to use the basic data type in a way to match the SYSTEM.Object base class interface, the system will automatically use it to be used like a general object. As follows:

INT n = 42;

INT32 IN = N;

The code to which the box is as follows:

INT n = 42;

INT32 IN = N;

// Note: Type conversion must be displayed when unpacking

INT N2 = (int) int32;

Some examples can be seen that everything is subject to the problem, C # and Java solutions are very similar,

Just C # encapsulates more operation details. But if there is a system to do these operations can improve the efficiency of the language itself? Microsoft's answer is definitely affirmative.

summary

The above is compared to C # and Java in compilation operation and the setting of the object, and found that C # simulates Java on the design, but in the specific implementation of C # packs more details, more details, compared to Java should It is easier to use, which is probably a Microsoft style.

转载请注明原文地址:https://www.9cbs.com/read-1207.html

New Post(0)