Wallace Wang

Beginning Programming All-in-One For Dummies


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

You can write a Java program correctly, compile it to bytecode format, and make the program run perfectly on a specific computer and operating system. But copy that same bytecode program to another computer, and suddenly, the Java program doesn’t run correctly. The problem can occur when the Java VM, on either computer, has errors in it. So, although Java programs are more portable than C/C++ programs, they still aren’t 100 percent portable.

      Besides creating full-fledged programs, like word processors or spreadsheets, Java can also create applets (smaller programs), which can be used to create interactive web pages.

If you’re looking for a programming language that makes programming safer and more portable, consider Java. Java programmers are in demand almost as much as C/C++ programmers, and the similarities between Java and C/C++ make it relatively easy to understand after you know C. (Or you can study Java first and then study C/C++ later.)

      Programming more safely with C#

      Microsoft took one look at C/C++ and decided it could create an improved language, which it dubbed C# (pronounced C-sharp).

      C# advantages

      C# has a couple advantages over languages such as C, C++, and even Java.

      IT’S OBJECT-ORIENTED

      One main advantage of C# over C++ is that C# is a true object-oriented programming language, so you have to use object-oriented programming to write a program in C#.

      Being forced to use only object-oriented programming techniques may seem like a drawback until you realize that C++ is a hybrid language that lets you choose whether to use object-oriented programming. Although C++ gives you, the programmer, more flexibility, C++ programs can also be a mishmash of structured programming mingled in with object-oriented programming.

      Trying to decipher such a mix of programming techniques can be confusing. By forcing all programmers to use object-oriented programming (and isolate their structured programming techniques only inside objects), C# programs can be much easier to understand and modify.

      IT’S TYPE-SAFE

      A second advantage of C# is that it’s a type-safe language. Basically, if a C# program stores data, such as a whole number (such as 3 or 49, but not 5.48), the C# compiler checks to make sure no other part of the program accidentally changes that whole number into a decimal.

      With languages that aren’t type-safe, the compiler lets a program change data types, such as storing a decimal or negative number where the program expects a whole number. Obviously, if your program is expecting a whole number but instead receives a decimal number, the program may get confused and crash.

      THE PROS AND CONS OF TYPE-SAFE LANGUAGES

      So, why isn’t every programming language type-safe? Good question. Here are two reasons:

       Creating a type-safe language means more work to create a compiler that can examine an entire program and check to make sure data types (such as numbers and text) aren't getting changed around unexpectedly. This translates into a slower and more complicated compiler, which is more work for the programmers who have to create the compiler in the first place.

       Type-safe languages can be restrictive, like trying to ride a motorcycle in a padded suit. The padded suit may protect you, but it also restricts your movement. Similarly, by not checking that data types remain consistent throughout a program, other languages give the programmer more freedom.Use this freedom wisely and you can create programs without the nuisance of type-safe checking, which can feel like having your parents staring over your shoulder every time you browse the Internet. Use this freedom poorly, and you’ll wind up writing a program that crashes the computer.

      .NET compatibility

      Because Microsoft invented C#, it also invented a special program — the .NET framework. The idea behind the .NET framework is that instead of compiling a C# program into machine language, you compile a C# program into p-code or Common Intermediate Language (CIL), which is similar to the bytecode intermediate file format of Java.

      The .NET framework allows you to both

       Run C# programs on any computer with the .NET framework.

       Write programs in multiple languages that all link together through the .NET framework, as shown in Figure 3-5.

Schematic illustration of the .NET framework can tie programs, written in multiple languages, into a single program.

      FIGURE 3-5: The .NET framework can tie programs, written in multiple languages, into a single program.

      

The only programming languages you can use with the .NET framework are languages specifically designed to work with the .NET framework. So, if you want to write a program using a combination of C# and BASIC, you have to find a BASIC compiler that works with the .NET framework, such as Microsoft’s own Visual Basic language.

      A final advantage of the .NET framework is that it lets you use event-driven programming to create your UI and then write event handlers in any .NET language, such as C#.

      Because C# is similar to C, C++, and Java, you can study C# first and then study the other languages (or vice versa).

      For that reason, many programmers prefer to first understand C or C++, and then understand C#.

      Choosing a curly-bracket language

       Knowing C can be great because it’s the basis for all the other languages. Plus, while figuring out C, you can get used to its cryptic syntax without having to worry about understanding object-oriented programming at the same time.

       Begin with C++ if you want to get started using object-oriented programming based on your knowledge of C. While figuring out C++, you can ignore its object-oriented features. After you feel comfortable with writing C++ programs, you can gradually start developing object-oriented programming techniques as well.

       If you want to write programs that can run on different computers, use Java. Java forces you to know object-oriented programming right from the start (like C#), so knowing Java means you can figure out object-oriented programming at the same time. Because Java isn’t as confusing as C or C++, understanding Java first is likely much easier than understanding C or C++.

       If you want to learn a safer version of C, consider trying C# or Python. The C# language is quickly becoming the standard language for writing Windows programs, while Python is popular for being easier to learn than C and being nearly as versatile as C.