Beginning Programming All-in-One For Dummies. Wallace Wang

Чтение книги онлайн.

Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 39

Beginning Programming All-in-One For Dummies - Wallace Wang

Скачать книгу

target="_blank" rel="nofollow" href="https://developer.apple.com/xcode">https://developer.apple.com/xcode), which only runs on a Mac. Another free option from Apple is Swift Playgrounds (www.apple.com/swift/playgrounds), which runs on a Mac or iPad and lets you write Swift code.

      Because Linux is free, most Linux compilers are free, too, including the popular GCC compiler (https://gcc.gnu.org), although you can buy commercial compilers if necessary.

      Interpreters are commonly used for scripting languages, such as Perl or Python, but they’re rarely used for system programming languages, such as C++. That’s because if you write a program and use an interpreter, you must distribute a copy of your source code with the interpreter. Giving away your source code essentially gives away your program, so most commercial programs use a compiler instead.

      However, interpreters can be useful for learning a programming language. By using an interpreter, you can focus solely on learning a programming language without the distraction of creating a complete program.

      One popular type of interpreter is the online interpreter, which lets you use any browser to practice typing specific language commands. JDoodle (www.jdoodle.com) lets you select from dozens of popular and obscure programming languages such as C++ and Swift, along with less common languages like Forth and Smalltalk.

Snapshot shows the Flutter website lets you type and run code using any browser.

      FIGURE 4-3: The Flutter website lets you type and run code using any browser.

      Nearly every web browser comes with a JavaScript interpreter. Web designers use JavaScript for creating interactive web pages, verifying information typed on a web page (such as a username and password), or opening pop-up windows that display advertisements.

      Although JavaScript interpreters can be found in any web browser, you may have to download and install interpreters for other programming languages. Some popular programming languages for running programs on web servers (those computers responsible for displaying and retrieving information from web pages, such as shopping data) include

       Perl (www.perl.com)

       PHP (www.php.net)

       Python (www.python.org)

       Ruby (www.ruby-lang.org)

      Not only do the preceding four languages have free interpreters that you can copy, but their interpreters also run on different operating systems. That means a Perl or Ruby program written on a Windows computer should run identically if it’s copied and run on a Linux or Mac computer.

      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.

      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.

Schematic illustration of virtual machine acts like a combination of an interpreter and a compiler.

      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.

      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

Скачать книгу