Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.

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

Название: Beginning Programming All-in-One For Dummies

Автор: Wallace Wang

Издательство: John Wiley & Sons Limited

Жанр: Программы

Серия:

isbn: 9781119884422

isbn:

СКАЧАТЬ who listens to each sentence you speak and then translates that sentence into another language. Type a program command into an interpreter, and the interpreter immediately translates that command into its equivalent machine language command. Type in another command, and the interpreter translates that second command right away.

      The problem with interpreters is that they only store the equivalent machine language commands in memory instead of in a separate file like a compiler does. If you want to sell or distribute your program, you have to give people your source code, along with an interpreter that can convert your source code into machine language commands. Because giving away your source code essentially means giving away your program, everyone who wants to sell their programs uses a compiler instead of an interpreter.

      

The original reason why computer scientists developed interpreters was because compilers were so slow. But after computer scientists started creating faster compilers, most people stopped using interpreters and just used compilers. Nowadays, computer scientists use interpreters for running certain types of programming languages known as scripting languages. (Find out more about scripting languages in Book 1, Chapter 3.)

      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.

       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.

      Taking the time to understand

      Programming is a skill that anyone can acquire. Like any skill, the best way to understand is to take the time to experiment, make mistakes, and learn from your failures. Some programmers prefer to spend their time mastering a single programming language. Others prefer to master the intricacies of writing programs for a specific operating system, such as Windows. Still others spend their time discovering a variety of programming languages and writing programs for different operating systems.

      There is no right or wrong way to figure out programming. The only “right” way is the way that works for you. That’s why self-taught programmers can often write programs that are just as good as (or even better than) programs written by PhD computer scientists.

      Believe it or not, programming a computer is actually fairly straightforward. The hard part is trying to write a program that actually works.

      There’s a big difference between knowing how to program and knowing a specific programming language. This book describes how programming works, which means you’ll understand the principles behind programming no matter what programming language you decide to use.

      When you understand a specific programming language, you’ll figure out how to write a program using that language. Don’t confuse knowing how to program with knowing a programming language!

      When people learn to speak their native language, they often think their particular spoken language is the only way to talk. So, when they learn a foreign language, they try to speak the foreign language just like they speak their native language, but using different words. That’s why literal translations of foreign languages can sound so funny and awkward to a native speaker.

      That’s exactly the same problem with understanding programming. To understand programming, you have to use a specific programming language, but each programming language works a certain way. So, if you know how to write programs in the C programming language, you may mistakenly think that the way the C language works is the way computer programming also works, but that’s not true.

      Like human languages, programming languages differ wildly. Someone who knows how to write programs in the C language thinks differently about programming than someone who knows how to write programs in assembly language.

      To describe how programming works, this book uses a variety of examples from different programming languages. You don’t have to understand how each program example in this book works. Just understand that programming languages can look and solve identical problems in very different ways.

      First, try to understand general programming principles without worrying about the way a particular programming language works. Then try to understand how a particular programming language works. As long as you know how to keep these two topics separate, you can figure out how to program a computer without the distraction of knowing a particular programming language.

      Besides, programming languages rise and fall in popularity all the time, so if you know only one programming language, your skills may become obsolete within a few years.

      At one time, most programmers used assembly language. For programmers who wanted a simpler language, they used BASIC or Pascal. When BASIC and Pascal fell out of favor, programmers gravitated toward C. Because C was so dangerous to use, many people started using C++ and Java. Microsoft created its own version of C++ called C#. Apple initially adopted a language similar to C++ called Objective-C, but then it created its own language called Swift. After running into legal problems using Java, Google adopted Kotlin as the official language for writing Android apps.

      When learning any programming language, the only certainty is that new programming languages will appear all the time, so you should СКАЧАТЬ