开发者

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

My professor asked us this question: What are the differences between a 开发者_如何学运维C#(.Net) Compiler and Java Compiler Technologies?


Both the Java and C# compilers compile to an "machine code" for an intermediate virtual machine that is independent of the ultimate execution platform; the JVM and CLR respectively.

JVM was originally designed solely to support Java. While it is possible to compile languages other than Java to run on a JVM, there are aspects of its design that are not entirely suited to certain classes of language. By contrast, the CLR and its instruction set were designed from day one to support a range of languages.

Another difference is in the way that JIT compilation works. According to Wikipedia, CLR is designed to run fully compiled code, so (presumably) the CLR's JIT compiler must eagerly compile the entire application before starting. (I also gather that you can compile the bytecodes to native code ahead of time.) By contrast, the Hotspot JVMs use true "just in time" compilation. Bytecode methods are initially executed by the JVM using a bytecode interpreter, which also gathers trace information about execution paths taken within the method. Those methods that are executed a number of times then get compiled to native code by the JIT compiler, using the captured trace information to help in the code optimization. This allows the native code to be optimized for the actual execution platform and even for the behaviour of the current execution of the application.

Of course, the C# and Java languages have many significant differences, and the corresponding compilers are different because of the need to handle these linguistic differences. For example, some C# compilers do more type inferencing ... because the corresponding C# language version relies more on inferred types. (And note that both the Java and C# languages have evolved over time.)


In terms of compiler, the largest difference I can think of (except the obvious "inputs" and "outputs") is the generics implementation, since both have generics, but very different (type erasure vs runtime-assisted). The boxing model is obviously different, but I'm not sure that is huge for the compiler.

The are obvious difference in features in terms of anonymous methods, anonymous inner classes, lambdas, delegates, etc but that is hard to compare 1:1. Ultimately, though, only your professor knows the answer he is looking for (and all due respect to professors, but don't necessarily be surprised if his answer is a year-or-more out of date with the bleeding edge).


One difference is that the C# compiler has some type inferencing capabilities that a Java compiler wouldn't have (although Java 7 may change this). As a simple example, in Java you have to type Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); while in C# you can use var anagrams = new HashMap<String, List<String>>(); (although you can create very large, complex expressions in C# without ever having to name a type).

Another difference is that the C# compiler can create expression trees, enabling you to pass descriptions of a function to another function. For example, (Func<int,int>) x => x * 2 is a function that takes an int and doubles it, while (Expression<Func<int,int>>) x => x * 2 is a datastructure that 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).


http://www.scribd.com/doc/6256795/Comparison-Between-CLR-and-JVM

i think this will give you an basic idea

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜