3

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?

2
  • .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. Commented Oct 31, 2013 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 :-) Commented Oct 31, 2013 at 21:36

2 Answers 2

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.

1
  • 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). Commented Oct 31, 2013 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.

2
  • 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. Commented Oct 31, 2013 at 20:35
  • 1
    In my book they are better. Justification: stackoverflow.com/questions/355060/c-sharp-vs-java-generics Commented Nov 1, 2013 at 7:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.