Which two programming languages require the program to be converted into executable code using a compiler? (Choose two.)
- PowerShell
- VBScript
- C#
- Java
- Python
The correct answers are:
- C#
- Java
Introduction to Compiled Programming Languages
When we talk about programming languages that require the program to be converted into executable code using a compiler, we are referring to languages that undergo a transformation process called compilation. In this process, the entire source code written by the programmer is translated into machine code or an intermediate language, which can then be executed by the computer’s processor. This compilation is done by a special software called a compiler.
Languages like C# and Java are compiled languages, meaning that the source code written in these languages must go through the compilation process before it can be executed. Other languages, such as Python, VBScript, and PowerShell, typically use an interpreter, which executes code line by line without requiring compilation into a standalone executable.
Let’s explore how compilation works in C# and Java, and why these two languages are considered compiled languages.
1. C# (C-Sharp)
C# is a popular, modern, object-oriented programming language developed by Microsoft as part of its .NET ecosystem. Like other languages in the C-family, C# is statically typed and compiled, meaning that the code must be converted into an intermediate or machine code before it can run.
Compilation Process in C#
When writing a C# program, the source code (which is written in files with the .cs
extension) is not directly executed. Instead, it must go through a compilation process using a C# compiler, such as the one included in Microsoft’s Visual Studio or the open-source Roslyn compiler. Here’s how the process works:
- Source Code Compilation: When the C# compiler is invoked, it takes the human-readable source code and compiles it into an intermediate language called Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL). This intermediate code is platform-independent and can run on any system that has the .NET runtime installed.
- Execution via the .NET Runtime: The compiled CIL code is stored in files with the
.exe
or.dll
extensions. When the program is executed, the .NET runtime, specifically the Just-In-Time (JIT) compiler, converts the intermediate code into machine code specific to the operating system and hardware where the program is running. This final step allows the code to be executed on the processor.
Benefits of Compilation in C#
- Performance: Compiled code generally runs faster than interpreted code because the program is translated into machine code before execution, allowing it to run directly on the hardware.
- Error Detection: Compilation helps catch many types of errors (syntax, type mismatches, etc.) before the program runs, ensuring that the code is free of major issues before it is executed.
- Distribution: Once compiled, a C# program can be distributed as an executable file (such as
.exe
), which can run independently on any machine with the appropriate runtime environment installed.
C# and Cross-Platform Development
Although C# was originally designed for Windows and the .NET Framework, the introduction of .NET Core (now part of the larger .NET 5/6 framework) has made it possible for C# applications to run on other operating systems like Linux and macOS. Regardless of the platform, the same compilation process—converting source code into intermediate language, followed by JIT compilation—ensures cross-platform compatibility.
2. Java
Java is another statically typed, object-oriented programming language, initially developed by Sun Microsystems (now owned by Oracle). Java is famous for its “write once, run anywhere” philosophy, which is achieved through the compilation process.
Compilation Process in Java
In Java, the source code (written in .java
files) must be compiled into an intermediate code called bytecode, which can then be executed on any machine that has a Java Virtual Machine (JVM) installed. The compilation process in Java follows these steps:
- Source Code Compilation: Java source code is compiled using the Java Compiler (javac), which translates the human-readable Java code into platform-independent bytecode. The bytecode is stored in
.class
files. - Execution via the JVM: The bytecode is not directly executable by the computer’s hardware. Instead, the Java Virtual Machine (JVM) reads the bytecode and, through a process called Just-In-Time (JIT) compilation, translates the bytecode into machine code specific to the platform on which the JVM is running. This machine code is then executed by the computer’s processor.
Benefits of Compilation in Java
- Portability: The biggest advantage of Java’s compilation process is its platform independence. Java bytecode can run on any device or operating system that has a JVM, allowing Java programs to be highly portable across different platforms.
- Performance: Java, through JIT compilation, strikes a balance between interpreted and compiled languages. While not as fast as fully compiled languages like C or C++, Java bytecode is compiled to native machine code at runtime, which offers better performance than purely interpreted languages.
- Security: Since the bytecode runs within the JVM, Java offers a layer of abstraction between the code and the underlying system, enhancing security. The JVM also manages memory and other resources, reducing the risk of certain types of errors and vulnerabilities, such as buffer overflows.
Java’s Role in Enterprise and Mobile Development
Java is one of the most widely used languages in enterprise environments due to its reliability, scalability, and cross-platform capabilities. It is also the foundation for Android app development, as Android apps are written in Java (or Kotlin, which compiles to Java bytecode) and run on the Dalvik Virtual Machine or Android Runtime.
Other Languages in the List
Let’s explore why the other languages—PowerShell, VBScript, and Python—are not compiled languages but instead use interpretation:
PowerShell
PowerShell is a command-line shell and scripting language primarily used for task automation and configuration management on Windows. PowerShell scripts (.ps1
files) are typically interpreted, meaning that commands are executed line by line without a compilation step. PowerShell does include features for creating compiled executable files, but its standard mode of operation relies on interpretation, making it different from languages like C# and Java.
VBScript
VBScript (Visual Basic Scripting Edition) is a scripting language used mainly for automation tasks on Windows systems. Like PowerShell, VBScript is an interpreted language, meaning it executes code line by line without needing a compiler to generate an executable. VBScript scripts are commonly embedded in HTML pages or used in Windows scripting, but they are not compiled like C# or Java programs.
Python
Python is an interpreted, high-level, dynamically typed programming language known for its readability and versatility. While Python code is first compiled into bytecode (similar to Java), this bytecode is not platform-independent or distributed like an executable. Instead, Python’s bytecode is interpreted by the Python Interpreter at runtime. This makes Python a language that is more often associated with interpretation than traditional compilation into standalone executables.
Conclusion
Languages like C# and Java require compilation into an intermediate form (CIL for C# and bytecode for Java) before the program can be executed. This compilation process ensures improved performance, error checking, and portability. In contrast, languages like PowerShell, VBScript, and Python rely on interpretation, where the code is executed line by line without a separate compilation step. Understanding whether a language is compiled or interpreted helps developers choose the right tool for their needs, balancing performance, portability, and ease of use.