Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 90

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.

Beginning Programming All-in-One For Dummies

Подняться наверх