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

Combining a compiler with an interpreter to create p-code

Оглавление

Creating separate executable files for each processor can get clumsy, and giving away your source code with an interpreter may be unreasonable. A third approach is to compile your program into an intermediate format called bytecode or pseudocode (often abbreviated as p-code). Unlike compiling source code directly into machine language, you compile your program into a p-code file instead.

You can take this p-code file and copy it on any computer. To run a p-code file, you need a special p-code interpreter, or a virtual machine. The virtual machine acts like an interpreter and runs the instructions compiled into the p-code file.

The advantage of p-code is that you can distribute a single p-code version of your program, which can run on multiple computers. But P-code has a couple disadvantages:

 P-code programs don’t run as fast as programs compiled into machine language.

 If a computer doesn’t have the right virtual machine installed, it can’t run your program.

The most popular programming language that uses p-code is Java. After you write a Java program, you can compile it into a p-code file, which can run on any computer that has a copy of the Java virtual machine, such as Android, Linux, macOS, and Windows. Microsoft’s .NET framework is similar to p-code that (theoretically) lets you run a program on any computer that can run the complete .NET framework.

The theory behind p-code is that you write a program once, and you can run it anywhere. The reality is that every operating system has its quirks, so it’s more common to write a program and be forced to test it on multiple operating systems. More often than not, a p-code program runs perfectly fine on one operating system (like Windows) but suffers mysterious problems when running on another operating system (such as Linux). Languages, such as Java, are getting better at letting you run the same program on multiple operating systems without major modifications, but be careful because p-code doesn’t always work as well as you may think.

Beginning Programming All-in-One For Dummies

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