3

This question already has an answer here:

I can understand the benefits of Java running on a JVM. Portability. Nice simple reason. But I have always been puzzled as to why Microsoft brought out their own version of a JVM - .NET. C# is supposed to be a fine language (haven't used myself) but could Microsoft have launched product to use native. ie to generate an exe?

My colleague is learning F#. The reason it has to be a language which runs on .NET is because the Microsoft Lync API which will be used is only available on .NET. ie there is no C API for Lync.

A cynical view may be that the reason is vendor lockin. F# will only run on a Microsoft platform (or C# for that matter) and so program is locked in. But maybe I am missing some other benefit of a VM platform?

marked as duplicate by gnat, jk., user7043, user53019, Kilian Foth Nov 1 '13 at 9:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • .NET executables run on many flavours of windows (8.1, 8, 7, ...) and bitness, and on phone, and perhaps other platforms as well. As a "party trick" I have run them on Unix. A native exe could not do that. – Kate Gregory Oct 31 '13 at 19:00
  • Microsoft did launch a product to generate an exe from C#. The .NET pre-use IL-to-native compiler is what consumes all your CPU time after a Windows Update run completes :-) – Ross Patterson Oct 31 '13 at 21:36
9

One of the most important aspects of the .NET virtual machine is the reason why it is called the Common Language Runtime.

All .NET programming languages compile to the same bytecode format which is executed in the same VM. This allows programs where different parts are written in different programming languages to interoperate seamlessly.

Virtual machines also have performance advantages, because they allow just-in-time compilation and just-in-time optimization. A common offline compiler can only optimize for a specific CPU architecture. When an application is distributed and the user doesn't have exactly the same system the application was optimized for, they will experience slightly worse performance than possible. But a JIT compiler can check the architecture when the program is executed and make full use of the features available. Also, a common offline optimizer can only make educated guesses which parts of a program are executed most often and thus have to be preferred during optimization. But a JIT optimizer can monitor the program while it is running, check which branches are used most often, and optimize the program on-the-fly while it is running.

But the Java VM can do all of that too. Why isn't Microsoft targeting the JVM then? It's for business reasons. Microsoft is known for wanting as much control as possible over the whole stack an application runs on. Having Sun (now Oracle) control a major part of the software development ecosystem was considered a problem for them. Especially when it allows software to run on non-Microsoft operating systems! So they developed their .NET framework as a direct competition to Java to attack Sun's (now Oracle's) market share.

  • 5
    Microsoft had a fairly significant (anti-trust) smack down when it came to the jvm when they did the Microsoft Java Virtual Machine and implemented incompatible changes (things from Sun JVM could run on it, but things compiled for it couldn't run on the Sun JVM). – user40980 Oct 31 '13 at 15:19
0

CLR also has value types (ie. structs on the stack) so one can argue it's a more efficient version of JVM. The implementation of generics is also better.

  • 1
    (-1) The CLR implementation is not necessarily "Better"; its different. JVM uses Type erasure which means all previously non Generic collections could be upgraded without duplicating all existing collections, as well as code that uses those types. In .net->clr they created duplicate types to support generics (See List<T> and ArrayList) and neglected to upgrade the existing collections (for backward compatibilty reasons) in the .net 2.0 framework and beyond. As a result many of the implementations using collections are not type-safe. Examples: All of asp.net and WinForms. – leat Oct 31 '13 at 20:35
  • 1
    In my book they are better. Justification: stackoverflow.com/questions/355060/c-sharp-vs-java-generics – Karl Nov 1 '13 at 7:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.