Important Features of the Java Language
If you believe the marketing hype put out by Sun and others, you'd think that Java is the best thing to happen to computers since the invention of memory. Java may not be that revolutionary, but Java does have many builtin features that set it apart from other languages (with the possible exception of Microsoft's C#, which is basically a rip-off of Java). The following sections describe just three of the many features that make Java so popular.
Type checking
All programming languages must deal in one way or the other with type checking-how a language handles variables that store different types of data. For example, numbers, strings, and dates are commonly used data types available in most programming languages. Most programming languages also have several different types of numbers, such as integers and real numbers.
All languages must check data types-so make sure you don't try to do things that don't make sense (such as multiplying the gross national product by your last name). The question is, does the language require you to declare every variable's type so you can do type checking when it compiles your programs, or does the language do type checking only after it runs your program?
Some languages, such as Basic, do almost no type checking at compile time. For example, in Microsoft's Visual Basic for Applications (VBA), you can assign any type of data to a variable. Thus the following statements are all allowed:
Let A = 5
Let A = "Strategery"
Let A = 3.14159
Here three different types of data-integer, string, and decimal-have been assigned to the same variable. This flexibility is convenient, but comes with a price. For example, the following sequence is perfectly legal in VBA:
Let A = 5
Let B = "Strategery"
Let C = A * B
Here, an integer is assigned to variable A, and a string is assigned to variable B. Then the third statement attempts to multiply the string by the integer. You can't multiply strings, so the third statement fails.
Java, on the other hand, does complete type checking at runtime. As a result, you must declare all variables as a particular type so the compiler can make sure you use the variables correctly. For example, the following bit of Java code won't compile:
int a = 5;
String b = "Strategery";
String c = a * b;
If you try to compile these lines, you get an error message saying that Java can't multiply an integer and a string.
In Java, every class you define creates a new type of data for the language to work with. Thus, the data types you have available to you in Java aren't just simple predefined types, such as numbers and strings. You can create your own types. For example, if you're writing a payroll system, you might create an Employee type. Then you can declare variables of type Employee that can only hold Employee objects. This prevents a lot of programming errors. For example, consider this code snippet:
Employee newHire;
newHire = 21;
This code creates a variable (newHire) that can hold only Employee objects. Then it tries to assign the number 21 to it. The Java compiler won't let you run this program because 21 is a number, not an employee.
Confused yet? If so, that's my fault. Inheritance is a pretty heady topic for Chapter 1 of a Java book. Don't panic if it makes no sense just yet. It will all be clear by the time you finish reading Book III, Chapter 4, which covers all the subtle nuances of using inheritance.
Automatic memory management
Memory management is another detail that all programming languages have to deal with. All programming languages let you create variables. When you create a variable, the language assigns a portion of the computer's memory to store the data referred to by the variable. Exactly how this memory is allocated is a detail that you can usually safely ignore, no matter which language you're working with. But a detail that many languages do not let you safely ignore is what happens to that memory when you no longer need the data that was stored in it.
In C++ and similar languages, you had to write code that explicitly released that memory so that other programs could access it. If you didn't do this, or if you did it wrong, your program might develop a memory leak, which means that your program slowly but surely sucks memory away from other programs until the operating system runs out of memory and your computer grinds to a halt.
In Java, you don't have to explicitly release memory when you're done with it. Instead, memory is freed up automatically when it is no longer needed. The Java Virtual Machine includes a special process called the garbage collector that snoops around the Virtual Machine's memory, determines when data is no longer being used, and automatically deletes that data and frees up the memory it occupied.
TECHNICAL STAUFF | A feature related to garbage collection is bounds checking, which guarantees that programs can't access memory that doesn't belong to them. Languages such as C or C++ don't have this type of safety. As a result, programming errors in C or C++ can cause one program to trample over memory that's being used by another program. That, in turn, can cause your whole computer to crash. |
Exception handling
As Robert Burns said, "The best-laid schemes of mice and men gang oft agley, an' leave us nought but grief and pain for promised joy." (Well, maybe that's not exactly what he said, but pretty close.) When you tinker with computer programming, you'll quickly discover what he meant. No matter how carefully you plan and test your programs, errors happen. And when they do, they threaten to grind your whole program to a crashing halt.
Java has a unique approach to error handling that's superior to that found in any other language (except, as I've mention a few times, C#, which just copies Java's approach). In Java, the Java Runtime Environment intercepts and folds errors of all types into a special type of object called an exception object. After all, Java is object-oriented through and through, so why shouldn't its exception-handling features be object-oriented?
Java requires any statements that can potentially cause an exception to be bracketed by code that can catch and handle the exception. In other words, you as the programmer must anticipate errors that can happen while your program is running, and make sure that those errors are properly dealt with. Although this necessity can sometimes be annoying, the resulting programs are more reliable.
No comments:
Post a Comment