Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I get confused when people try to make a distinction between compiled languages and managed languages. From experience, I understand that most consider compiled languages to be C,C++ while managed languages are Java,C# (There are obviously more, but these are just few examples). But what exactly is the core difference between the two types of languages?

My understanding is that any program, regardless of what language you use is essentially "compiled" into a low-level machine code which is then interpreted, so does that kinda make managed languages a subset of compiled languages (That is, all managed languages are compiled languages but not the other way around)?

share|improve this question
2  
this term is coined by Microsoft, in narrow sense Java is managed as well. Almost in all cases we can think of managed languages like of a subset of that one that compiles.Also, this, I believe is related - programmers.stackexchange.com/questions/72446/… –  shabunc Sep 10 '12 at 9:12
    
Note that there's a huge difference between a language that compiles (statically) the code to something more machine manageable before runtime (like Java) and one that does it at runtime (like Python). One major difference is that statically compiling it before runtime gives the comiler a chance to make some optimizations which may result in hefty speed improvements (like Java's JIT, rearranging the code for branch prediction etc). –  Shivan Dragon Sep 10 '12 at 10:53
5  
@ShivanDragon, a "language" does not compile anything. Its implementation does. And you can compile Python statically (see PyPy or IronPython for example). OTOH, it is really hard to do it efficiently with a dynamically typed language (look up "tracing JIT", "abstract interpretation", etc.) –  SK-logic Sep 10 '12 at 11:04
    
@SK-logic: agreed, I've said a baddie. I wanted to refer to the platform, not the language. –  Shivan Dragon Sep 10 '12 at 11:06
    
@shabunc Actually I'd say Compiled is a subset of Managed. A Managed language can do anything a compiled language can do (at virtually the same speed), and then more since a Managed language can be compiled. In order to give C the features of a managed language, you'd need to build a "VM" and actually make it a managed language. –  Bill K Sep 10 '12 at 17:51

3 Answers 3

up vote 36 down vote accepted

The difference is not in "compiled" vs. "managed", these are two orthogonal axes. By "managed" they normally mean a presence of a garbage-collected memory management and/or a presence of a virtual machine infrastructure. Both has absolutely nothing to do with compilation and whatever people deem to be opposite to it.

All this "differences" are quite blurred, artificial and irrelevant, since it is always possible to mix managed and unmanaged memory in a single runtime, and a difference between compilation and interpretation is very vague too.

share|improve this answer
    
This is what I basically had in mind, but I've come across a lot of people who keeps making this distinction. Thanks for the clear answer. –  l46kok Sep 10 '12 at 9:26

I think there is a distinction to be made, however it is not necessarily between "Compiled" and "Managed". These are not opposites; a language can be compiled and not managed, or interpreted (not compiled) and managed, or both, or even neither.

A "compiled" language is simply one in which there is a step that transforms the source code written by the developer into some more regular "bytecode" which is what is executed by the machine. The "machine" can be the actual processor, or a "virtual machine" that performs additional operations on the bytecodes to translate them to "native" machine instructions. The antonym for a "compiled" language is an "interpreted" language, in which the source code is transformed into bytecode instructions at runtime, without a compilation step.

A "managed" language is a language designed to produce programs that are consumed within a specific runtime environment, which almost always includes a bytecode interpreter; a "virtual machine" that takes the program's code and performs some additional machine or environment-specific transformation. The environment may also include memory management, such as a "garbage collector" and other "security" features meant to keep the program operating within its "sandbox" of space and tools, however such features are not the sole domain of "managed" runtimes. Virtually all interpreted languages could be considered managed, because they require the interpreter to be running underneath the lines of "user" code being executed. In addition, JVM and .NET languages (Java, Scala, C#, VB, F#, IronWhatever) are compiled into an intermediate language or IL, which is superficially similar in form and function to a binary assembly language, but doesn't adhere 100% to any "native" instruction set. These instructions are executed by the JVM, or by .NET's CLR, which effectively translates them to native binary instructions specific to the CPU architecture and/or OS of the machine.

So, languages can generally be described as "compiled" or "interpreted", and as "unmanaged" (or "native") and "managed". There are languages that can be described as any combination of these except possible "interpreted native"; if you consider the interpretation layer as a "runtime" (which is easy to argue for and hard to argue against), then all interpreted languages are "managed".

If you want to get technical, almost all programs targeting a multitasking OS nowadays are "managed"; the OS will create a "virtual machine" for each program that is running, in which the program thinks (or at least doesn't have to know otherwise) that it is the only thing running. The code may make calls within itself and to other referenced libraries as if that program was the only thing loaded in memory; similarly, calls to allocate RAM and other higher memory to store and manipulate data and control devices are coded as if the entire memory architecture was available. The VM (and the OS behind it) then translates various memory pointers to the actual location of the program, its data, and hooks to device drivers etc. This is most often done by applying a memory offset (each VM gets a block of 2GB or whatever of memory, starting at address X which the program can treat as if that X was address 0) and as such is very cheap to do, but there are other things the OS kernel is responsible for, such as process scheduling and inter-process communication, which are trickier to manage. However, this basic pattern is generally not considered "managed", as the program doesn't have to know that it's being run by a virtual machine. A program that was designed to be run on the MS-DOS command line can be run on newer Windows OSes that don't even have the MS-DOS environment underneath them anymore; the program is instead given a "virtual console" environment, and provided it doesn't try to leave this "sandbox" by trying to directly access protected areas of memory, it will run quite happily.

share|improve this answer
    
"languages can generally be described as "compiled" or "interpreted"" – No, they can't. Compilation and interpretation are traits of, well, compilers and interpreters not languages. The term "compiled language" doesn't even make sense. If English were a typed language, it would be a type error. –  Jörg W Mittag Sep 10 '12 at 23:44
1  
Compilers and interpreters are usually found compiling and interpreting very specific dialects of languages that are designed to undergo either compilation or interpretation. Nobody's compiling JavaScript source code that I know of, and nobody's interpreting C#. The languages are designed to be consumed in one way or the other. As such, it is usually acceptable to refer to the language itself as "compiled" or "interpreted" because the full environment in which the language is utilized involves one of those two steps. –  KeithS Sep 10 '12 at 23:53
    
    
en.wikipedia.org/wiki/Interpreted_language - "Theoretically, any language may be compiled or interpreted, so this designation is applied purely because of common implementation practice and not some essential property of a language." –  KeithS Sep 10 '12 at 23:57
    
@KeithS, wikipedia is not nearly perfect. The fact that an article exist for a certain invalid term does not make this term any more valid. Yes, languages are always designed with a certain mode of execution in mind, but it is still counterproductive to brand them "compiled" or "interpreted" solely on a basis of their designers intention. And, as for an interpretation, it is really hard to find a proper "interpreter" any way. Tcl is probably the last of its kind. All the other so called "interpreters" are in fact compilers. –  SK-logic Sep 11 '12 at 8:34

To quote Wikipedia:

Managed code is a term coined by Microsoft to identify computer program source code that requires and will only execute under the management of a Common Language Runtime virtual machine (resulting in bytecode).

Managed code needs a runtime (like the .NET CLT) to execute.

share|improve this answer
2  
Managed code has nothing to do with a framework. It needs a runtime that manages memory. –  Oded Sep 10 '12 at 8:57
    
Perhaps my wording is a bit off, but isn't the .NET framework actually a "common language runtime"? –  janvdl Sep 10 '12 at 8:58
1  
No. In includes the CLR, but it also includes the Base Class Libraries, the IL specification and more. –  Oded Sep 10 '12 at 9:00
    
Thanks, edited my original post. –  janvdl Sep 10 '12 at 9:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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