align="center">
THE ADVANTAGES OF INTERPRETED LANGUAGES
A program run by an interpreter is almost always slower than the same program compiled into machine language, so why not compile every language rather than run them under an interpreter?
One of the reasons is that creating a compiler for multiple operating systems is much more difficult than creating an interpreter for multiple operating systems. To create a compiler, you need to know how to translate a programming language into machine code, but because operating systems can run under different processors (such as the ARM or Intel processors), you have to translate language commands into completely different machine language commands. Creating a compiler that works correctly for one processor is hard enough, but creating that same compiler to work under multiple processors identically and error-free is much more difficult.
Compiling a program into machine language is great when you want to distribute a program to others. However, languages like Perl or Ruby are often used to create short programs that run on a web server. Using an interpreter may run a program more slowly, but you can write a program and run it right away without compiling it first. Also, by running the source code directly, interpreters let you see the source code of each program that’s running, so you can edit that source code and see how your changes affect the program. You can still do this with a compiler, but having to compile a program and then store a separate executable version of that program is a minor annoyance that you can avoid completely just by using an interpreter.
Compilers are great for distributing programs. Interpreters are much better for writing and running shorter programs when you don’t care whether anyone can see or copy the source code.
Compiling to a Virtual Machine
The problem with compilers is that they’re difficult to make for multiple operating systems and processors. The problem with interpreters is that they need the source code of a program to run, making interpreters unsuitable for distributing software. To solve both these problems, computer scientists created a third alternative — a virtual machine (VM).
To speed up programs, computer scientists have developed just-in-time (JIT) compilers. These types of compilers translate code into native code on the fly, making programs run faster than ordinary interpreted programs running on a virtual machine.To protect the source code of a program, a VM lets you compile your program into an intermediate file called bytecode or pseudocode (also known as p-code). To make a program run on multiple operating systems, you need a VM that runs on each operating system, as shown in Figure 4-4.
FIGURE 4-4: A virtual machine acts like a combination of an interpreter and a compiler.
When you compile a program into bytecode, it’s still possible to disassemble (reverse-engineer) that bytecode file and view the original source code.
The most popular programming language that uses a VM is Java (www.oracle.com/java/technologies
), which was created by Sun Microsystems and is now owned by Oracle. The idea behind Java is to let you write a single program in Java, compile it into a bytecode file, and then distribute that bytecode file to any computer that has a Java VM installed.
Theoretically, you can write a program once and make it run on Linux, macOS, and Windows with no modifications whatsoever. Realistically, you may still need to tweak the program a bit to get it to run flawlessly on different operating systems, but that’s still much easier than writing a program from scratch for another operating system.
Despite these drawbacks, Java has grown in popularity. Many companies write and sell programs entirely written in Java. As computers get faster and Oracle improves the performance of its VM, programs written in Java probably will run fast enough for most uses.
Writing a Program with an Editor
To write programs, you need an editor, which acts like a special word processor just for helping you write commands in your favorite programming language. After you type your program commands in an editor, you can save this file (known as the source code). Then, you can feed this source code file into a compiler to turn it into a working program.
You can choose from two types of editors: stand-alone or integrated development environment (IDE). Your best bet probably depends on whether you write programs in more than one programming language.
Stand-alone editors
A stand-alone editor is nothing more than a separate program that you run when you want to edit your program. You run the compiler separately.
If you regularly write programs in different programming languages, you may want to use a stand-alone editor. That way you can get familiar with all the features of a single editor.
You can buy stand-alone editors, but here are two popular free ones; both of these editors run on multiple operating systems (such as Linux, macOS, and Windows):
GNU Emacs (www.gnu.org/software/emacs/emacs.html
)
VIM (www.vim.org
)
COMMON EDITOR FEATURES
Whether you prefer a stand-alone editor or an integrated development environment, most editors offer the following features:
Multiple undo/redo commands let you experiment with making changes to your source code. If they don’t work out, you can undo your changes. Typically, editors let you undo a large number of changes you made, such as the last 100 changes.
Multiple file editing comes in handy so you can view different files in separate windows and copy code from one window to another, or just study how one part of your program will interact with another part of that same program.
Syntax completion and highlighting is when the editor recognizes certain programming languages, such as C++ and Java. The moment you type a valid language command, the editor can finish typing that command for you at the touch of a button, thereby saving you time. So, if you type a typical if-then statement, the editor automatically types in a generic if-then statement (complete with necessary parentheses), so you just type in the actual data to use.
Syntax highlighting occurs after you write a program; the editor highlights valid language commands to help you separate language commands from any data and commands you create. Without syntax highlighting, source code can look like a mass of text. With syntax highlighting, source code can be easier to read and understand.
Automatic indentation and parentheses matching where indentation can align your code to make it follow a standard indentation format while parentheses matching helps you identify where an opening parenthesis, square bracket, or curly bracket begins and ends.
Macros let you customize the editor and essentially