What are the differences between a C # compiler (. Net) and Java Compiler Technologies?

My professor asked us this question: what are the differences between the C # compiler (. Net) and the Java compilers?

+4
source share
4 answers

The Java and C # compilers are compiled into machine code for an intermediate virtual machine that is independent of the final execution platform; JVM and CLR respectively.

The JVM was originally designed solely to support Java. Although you can compile languages ​​other than Java to work on the JVM, there are aspects of its design that are not entirely suitable for some classes of the language. In contrast, the CLR and its instruction set were developed from day one to support a number of languages.

Another difference is how JIT compilation works. According to Wikipedia , the CLR is designed to run fully compiled code, so (presumably) the JR CLR compiler should readily compile the entire application before launching it, (I also intend that you can compile the bytecodes to your own code ahead of time.) In contrast, JVM Hotspot use true time-only compilation. The bytecode methods are initially executed by the JVM using the bytecode interpreter, which also collects information about the trace of the execution paths accepted inside the method. Those methods that are executed several times are then compiled into native code by the JIT compiler, using the obtained trace information to help optimize the code. This allows you to optimize your own code for a real execution platform, and even for the behavior of the current application execution.

Of course, the C # and Java languages ​​have many significant differences, and the corresponding compilers differ from each other due to the need to handle these language differences. For example, C # compilers do more types of output ... because C # relies more on output types.

+2
source

In terms of the compiler, the biggest difference I can think of (except for the obvious “inputs” and “outputs”) is the implementation of generics, since both have generics, but very different ones (erasing the type vs runtime-assisted). The boxing model is obviously different, but I'm not sure if this is huge for the compiler.

There is an obvious difference in capabilities in terms of anonymous methods, anonymous inner classes, lambdas, delegates, etc., but it's hard to compare 1: 1. Ultimately, however, only your professor knows the answer he is looking for (and all due respect to the professors, but it is not necessary to be surprised if his answer is a year or more out of date with the edge of the bleeding).

0
source

One of the differences is that the C # compiler has some type inference capabilities that the Java compiler did not have (although Java 7 may change that). As a simple example in Java, you need to enter Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); and in C # you can use var anagrams = new HashMap<String, List<String>>(); (although you can create very large complex expressions in C # without having a type name).

Another difference is that the C # compiler can create expression trees, allowing you to pass function descriptions to another function. For example, (Func<int,int>) x => x * 2 is a function that takes an int and doubles it, and (Expression<Func<int,int>>) x => x * 2 is a data structure, which describes a function that takes an int and doubles it. You can take this description and compile it into a function (to run locally) or translate it into SQL (to run as part of a database query).

0
source
0
source

All Articles