Friday, November 06, 2009

Introduction


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.
C++, Java and C# are definitely three of the most widely used programming languages in the world. C++ extends the capabilities of C with object orientation capabilities. C is the ubiquitous language in system and low-level programming especially in the *nix system. Both Java and C# are derived from C++. Each of these languages has their strengths and weaknesses in regard of capability, flexibility, simplicity.




The main differences of theses languages actually lie in the compilation and execution processes. The compilation process is responsible for preparing the code to be runnable on the target platform. You have probably heard about "Managed Environment". C++ programs run in unmanaged runtime environment. The compiler and linker of C++ generate machine readable formats directly. The operating system is responsible for loading the executable program and run it.

Java and C# programs, on the other hand, run on managed environment. The programs are compiled into so called byte code. The Java or .Net runtime, called Virtual Machine (VM), loads the compiled file, interprets the byte code and converted to machine code by Just-In-Time (JIT) compiler, which in turn executed on top of the VM. The VM has complete control over the program and can monitor, investigate and occasionally manipulate it. It is possible to directly generate the machine readable byte code and thus escaping the JIT compilation step. But it is strictly discouraged.

Following activity diagram shows the C++ compilation, linking, and execution process. The compilation generates object codes, the linker creates runnable binary code (e.g. single .exe from multiple object files), and at execution time the operating system loads the file into memory and runs it.






The process has more steps in managed environment. The Java compiler generates byte code from the source. The byte code is human readable; there are even tools available to generate source classes from byte code. This is more or less similar with C# compilation. The generated code is called MS Intermediate Language (MSIL). All .Net aware compilers translate the source code into MSIL. The MSIL has common type system (CTS) for all languages. So at this level, program written in one language can be shared by other language, making .Net a programming language neutral framework.



No comments:

Post a Comment