Welcome to Java
This chapter is a gentle introduction to the world of Java. In the next few pages, you find out what Java is, where it came from, and where it's going. You also discover some of the unique strengths of Java-as well as some of its weaknesses. And I also compare Java to the other popular programming languages, including C, C++, C#, and Visual Basic.
By the way, I assume in this chapter that you have at least enough background to know what computer programming is all about. That doesn't mean that I assume you're an expert or professional programmer. It just means that I don't take the time to explain such basics as what a computer program is, what a programming language is, and so on. If you have absolutely no programming experience, I suggest you pick up a copy of Java For Dummies, 4th Edition, or Beginning Programming with Java For Dummies, 2nd Edition (both by Wiley Publishing, Inc.).
Throughout this chapter, you find little snippets of Java program code, plus a few snippets of code written in other languages like C, C++, or Basic. If you don't have a clue what this code means or does, don't panic. I just want to give you a feel for what Java programming looks like-and how it compares to programming in other languages.
REMEMBER |
All the code listings used in this book are available for download at http://www.dummies.com/go/javaaiofd2e. |
What Is Java, and Why Is It So Great?
Java is a programming language in the tradition of C and C++. As a result, if you have any experience with C or C++, you'll find yourself in familiar territory often as you learn the various features of Java. (For more information about the similarities and differences between Java and C or C++, see the section "Comparing Java to Other Languages" later in this chapter.)
However, Java differs from other programming languages in a couple of significant ways. The following sections describe the most important differences.
Platform independence
One of the main reasons Java is so popular is its platform independence, which means simply that Java programs can be run on many different types of computers. A Java program runs on any computer with a Java Runtime Environment, also known as a JRE, installed. A JRE is available for almost every type of computer you can think of, from PCs running any version of Windows, Macintosh computers, Unix or Linux computers, huge mainframe computers, and even cellphones.
Before Java, other programming languages promised platform independence by providing compatible compilers for different platforms. (A compiler is the program that translates programs written in a programming language into a form that can actually run on a computer.) The idea was that you could compile different versions of the programs for each platform. Unfortunately, this idea never really worked. The compilers were never completely identical on each platform-each had its own little nuances. As a result, you had to maintain a different version of your program for each platform you wanted to support.
Java's platform independence isn't based on providing compatible compilers for different platforms. Instead, Java is based on the concept of a virtual machine. You can think of the Java Virtual Machine (sometimes called the JVM) as a hypothetical computer platform-a design for a computer that doesn't really exist as actual hardware. Instead, the Java Runtime Environment is an emulator-a program that sets aside part of your hard drive to act like a computer (namely, the Java Virtual Machine) that can execute Java programs.
The Java compiler doesn't translate Java into the machine language of the computer that the program is running on. Instead, the compiler translates Java into the machine language of the Java Virtual Machine, which is called bytecode. Then the Java Runtime Environment runs the bytecode in the JVM. Because of the JVM, you can execute a Java program on any computer that has a Java Runtime Environment installed, without recompiling the program.
That's how Java provides platform independence-and believe it or not, it works pretty well. The programs you write run just as well on a PC running any version of Windows, a Macintosh, a Unix or Linux machine, or any other computer with a JRE installed.
While you lie awake tonight pondering the significance of Java's platform independence, here are a few additional thoughts to ponder:
- The JRE is separate from the Java compiler. As a result, you don't have to install a Java compiler to run compiled Java programs. All you need is the JRE.
- When someone asks whether your computer "has Java," they usually mean "Have you installed the Java Runtime Environment?" (You need the JRE so you can run Java programs.)
-
TECHNICAL STAUFF Platform independence only goes so far. If you have some obscure type of computer system-such as an antique Olivetti Programma 101-and a JRE isn't available for it, you can't run Java programs on it.
- If you're interested, the Java Virtual Machine is completely stack-oriented-it has no registers for storing local data. (I'm not going to explain what that means, so if it didn't make sense, skip it. It's not important. It's just interesting to nerds who know about stacks, registers, and things of that ilk.)
- Java's platform independence isn't perfect. Although the bytecode runs identically on every computer that has a JRE, some parts of Java use services provided by the underlying operating system. As a result, sometimes minor variations crop up, especially with applications that use graphical interfaces.
- Because a runtime system that emulates a Java Virtual Machine executes Java bytecode, some people mistakenly compare Java to interpreted languages, such as Basic or Perl. However, those languages aren't compiled at all. Instead, the interpreter reads and interprets each statement as it is executed. Java is a true compiled language-it's just compiled to the machine language of JVM rather than to the machine language of an actual computer platform.
-
TECHNICAL STAUFF I didn't make up the Olivetti Programma 101. It was a desktop computer made in the early 1960s, and happened to be my introduction to computer programming. (My junior high school math teacher had one in the back of his classroom and let me play with it during lunch.) Do a Google search for "Olivetti Programma 101," and you can find several interesting Web sites about it.
Object orientation
Java is inherently object-oriented, which means that Java programs are made up from programming elements called objects. Simply put (don't you love it when you read that in a computer book?), an object is a programming entity that represents either some real-world object or an abstract concept.
All objects have two basic characteristics:
- Objects have data, also known as state. For example, an object that represents a book has data such as the book's title, author, and publisher.
- Objects also have behavior, which means that they can perform certain tasks. In Java, these tasks are called methods. For example, an object that represents a car might have methods such as start, stop, drive, or crash. Some methods simply allow you to access the object's data. For example, a book object might have a getTitle method that tells you the book's title.
Classes are closely related to objects. A class is the program code you write to create objects. The class describes the data and methods that define the object's state and behavior. Then, when the program executes, classes are used to create objects.
For example, suppose you're writing a payroll program. This program probably needs objects to represent the company's employees. So, the program includes a class (probably named Employee) that defines the data and methods for each employee object. Then, when your program runs, it uses this class to create an object for each of your company's employees.
The Java API
The Java language itself is very simple. However, Java comes with a library of classes that provide commonly used utility functions that most Java programs can't do without. This class library, called the Java API, is as much a part of Java as the language itself. In fact, the real challenge of learning how to use Java isn't learning the language; it's learning the API. The Java language has only 50 keywords, but the Java API has several thousand classes-with tens of thousands of methods you can use in your programs.
For example, the Java API has classes that let you do trigonometry, write data to files, create windows on-screen, or retrieve information from a database. Many of the classes in the API are general purpose and commonly used. For example, a whole series of classes stores collections of data. But many are obscure, used only in special situations.
Fortunately, you don't have to learn anywhere near all of the Java API. Most programmers are fluent with only a small portion of it-the portion that applies most directly to the types of programs they write. If you find a need to use some class from the API that you aren't yet familiar with, you can look up what the class does in the Java API documentation at java.sun.com/javase/6/docs/api.
The Internet
Java is often associated with the Internet, and rightfully so. That's because Al Gore invented Java just a few days after he invented the Internet. Okay, Java wasn't really invented by Al Gore; Java was developed right at the time the World Wide Web was becoming a phenomenon-and Java was specifically designed to take advantage of the Web. In particular, the whole concept behind the Java Virtual Machine is to enable any computer connected to the Internet to run Java programs, regardless of the type of computer or the operating system it runs.
You can find two distinct types of Java programs on the Internet:
- Applets: These are Java programs that run directly within a Web browser. To run an applet, the browser starts a Java Virtual Machine, and that virtual machine is given a portion of the Web page to work with. Then the virtual machine runs the applet's bytecode.
- Servlets: These are Web-based Java programs that run on an Internet server computer rather than in an Internet user's Web browser. Servlets are the real way many, if not most, commercial Web sites work. Basically, a servlet is a program that generates a page of HTML that is then sent to a user's computer to be displayed in a Web browser. For example, if you request information about a product from an online store, the store's Web server runs a servlet to generate the HTML page that contains the product information you requested.
You find out how to create both types of applications in Book VII.
Comparing Java to Other Languages
Superficially, Java looks a lot like many of the programming languages that preceded it. For example, here's the classic Hello, World! program written in the C programming language:
main() { Printf("Hello, World!"); }
This program simply displays the text “Hello, World!” on the computer's console. Here's the same program (almost) written in Java:
public class HelloApp { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Although the Java version is a bit more verbose, the two have several similarities:
- Both require that each executable statement end with a semicolon.
- Both use braces ({}) to mark blocks of code.
- Both use a routine called main as the main entry point for the program.
There are many other similarities besides these that aren't evident in this simple example.
However, these two trivial examples bring the major difference between C and Java front and center: Java is inherently object-oriented. Object-oriented programming rears its ugly head even in this simple example:
- In Java, even the simplest program is a class, so you have to provide a line that declares the name of the class. In this example, the class is named HelloApp. HelloApp has a method named main, which the Java Virtual Machine automatically calls when a program is run.
- In the C example, printf is a library function you call to print information to the console. In Java, you use the PrintStream class to write information to the console. PrintStream? There's no PrintStream in this program! Wait a minute-yes, there is. Every Java program has available to it a PrintStream object that writes information to the console. You can get this PrintStream object by calling the out method of another class, named System. Thus, System.out gets the PrintStream object that writes to the console. The PrintStream class, in turn, has a method named println that writes a line to the console. So System.out.println really does two things, in the following order:
- It uses the out field of the System class to get a PrintStream object.
- It calls the println method of that object to write a line to the console.
Confusing? You bet. It will all make sense when you read about object-oriented programming in Book III, Chapter 1.
- void looks familiar. Although it isn't shown in the C example, you could have coded void on the main function declaration to indicate that the main function doesn't return a value. void has the same meaning in Java. But static? What does that mean? That, too, is evidence of Java's object orientation. It's a bit early to explain what it means in this chapter, but you can find out in Book II, Chapter 7.
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.
TECHNICAL STAUFF |
An important object-oriented programming feature of Java called inheritance adds an interesting-and incredibly useful-twist to type checking. Inheritance is way too complicated to dive into just yet, so I'll be brief here: In Java, you can create your own data types that are derived from other data types. For example, Employees are people. Customers are people too. So you might create a Person class and then create Employee and Customer classes that both inherit the Person class. Then you can write code like this: Person p; Employee e; Customer c; p = e; // this is allowed because an Employee is also a Person. c = e; // this isn't allowed because an employee is not a Customer. |
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.
On the Downside Java s Weaknesses
So far, I've been tooting Java's horn pretty loudly. Lest you think that learning Java is a walk in the park, the following paragraphs point out some of Java's shortcomings (note that many of these drawbacks have to do with the API rather than the language itself):
- The API is way too big. It includes so many classes and methods, you'll most likely never learn even half of them. And the sheer size of the Java API doesn't allow you to wander through it on your own, hoping to discover that one class that's perfect for the problem you're working on.
- The API is overdesigned. In some cases, it seems as if the Java designers go out of their way to complicate the things that should be simple to use. For example, the API class that defines a multi-line text input area doesn't have a scroll bar. Instead, a separate class defines a panel that has a scroll bar. To create a multi-line text area with a scroll bar, you have to use both classes. That's fine if you ever want to create a text area that doesn't have a scroll bar, but you never will. Java's designers complicated the design of the text area and scroll panel classes to provide for a case that no one ever uses or would want to use.
- Some corners of the API are haphazardly designed. Most of the problems can be traced back to the initial version of Java, which was rushed to market so it could ride the crest of the World Wide Web explosion in the late 1990s. Since then, many parts of the API have been retooled more thoughtfully. But the API is still riddled with remnants of Java's early days.
-
TECHNICAL STAUFF In my opinion, the biggest weakness of Java is that it doesn't directly support true decimal data. This issue is a little too complicated to get into right now, but the implication is this: Without special coding (which few Java books explain), Java doesn't know how to add. For example, consider this bit of code:
double x = 5.02; double y = 0.01; double z = x + y; System.out.println(z);
This little program should print 5.03, right? It doesn't. Instead, it prints 5.029999999999999. This little error may not seem like much, but it can add up. If you ever make a purchase from an online store and notice that the sales tax is a penny off, this is why. The explanation for why these errors happen-and how to prevent them-is pretty technical, but it's something every Java programmer needs to understand. You can find all the gory details in Bonus Chapter 1 on this book's Web site.
Java Version Insanity
Like most products, Java gets periodic upgrades and enhancements. Since its initial release in 1996, Java has undergone the following version updates:
- Java 1.0: The original release of Java in 1996. Most of the language itself is still pretty much the same as it was in version 1.0, but the API has changed a lot since this release.
- Java 1.1: This version was the first upgrade to Java, released in 1997. This release is important because most Internet browsers include builtin support for applets based on Java 1.1. To run applets based on later versions of Java, you must, in most cases, download and install a current JRE.
- Java 1.2: This version, released in late 1998, was a huge improvement over the previous version. So much so, in fact, that Sun called it "Java 2." It included an entirely new API called Swing for creating graphical user interfaces, as well as other major features.
- Java 1.3: This version, released in 2000, was mostly about improving performance by changing the way the runtime system works. Oddly, though this version is technically Java 1.3, it's also called "Java 2 version 1.3." Go figure.
- Java 1.4: Released in 2001, this version offered a slew of improvements. As you might guess, it is called "Java 2 version 1.4." Keep figuring….
- Java 1.5: Released in 2004, this version included more changes and improvements than any other version. To add to Sun's apparent unpredictability with its version numbering, this version officially has two version numbers. Sun's official Java Web site explains it like this:
"Both version numbers '1.5.0' and '5.0' are used to identify this release of the Java 2 Platform Standard Edition. Version '5.0' is the product version, while '1.5.0' is the developer version."
That clears it right up, doesn't it?
- Java 1.6: Released in December 2006 (just in time for the holidays!), this version of Java is the latest and greatest. The improvements in Java 1.6 aren't as earth shattering as the Java 1.5 improvements, but Java 1.6 runs faster and better than any previous version of Java.
For Java 1.6, the product version is "6" (not "6.0"). And remember the extra "2" that appeared magically in 1998? Well, the "2" is gone in Java 1.6. So-unlike the versions between 1998 and 2006-Java 1.6 is officially named the Java Platform (not the Java "2" Platform). Personally, I think someone at Sun has been talking to George Lucas. I fully expect the next version of Java to be a prequel, called "Java 0 Episode 1."
- Anyway, throughout this book I use the version numbers 1.6 and 6 interchangeably to mean the current version. Of course, Sun isn't finished with Java. The next version, 7 (or 1.7.0), is due in 2008.
You may need to be aware of version differences if you're writing applications that you want to be able to run on earlier versions of Java. Bear in mind, however, that one of the chief benefits of Java is that the runtime system is free and can be easily downloaded and installed by end users. As a result, you shouldn't hesitate to use the features of Java 1.6 when you need them.
What s in a Name?
The final topic I want to cover in this chapter is the names of the various pieces that make up Java's technology-specifically, the acronyms you constantly come across whenever you read or talk about Java, such as JVM, JRE, JDK, J2EE, and so on. Here they are, in no particular order of importance:
- JDK: The Java Development Kit-that is, the toolkit for developers that includes the Java compiler and the runtime environment. To write Java programs, you need the JDK. This term was used with the original versions of Java (1.0 and 1.1) and abandoned with version 1.2 in favor of SDK. But with versions 5.0 and 6, the term JDK is officially back in vogue.
- SDK: The Software Development Kit-what Sun called the JDK for versions 1.2, 1.3, and 1.4.
- JRE: The Java Runtime Environment-the program that emulates the JVM, so that users can run Java programs. To run Java programs, you need only download and install the JRE.
- JVM: The Java Virtual Machine-the platform-independent machine that is emulated by the JRE. All Java programs run in a JVM.
- Java SE: Java Standard Edition-a term that describes the Java language and the basic set of API libraries that are used to create Windows and applet applications. Most of this book focuses on Java SE.
- J2SE: Java 2 Standard Edition-an older term for the Java language and basic libraries (for Java versions 1.2 through 1.5).
- Java EE: Java Enterprise Edition, also known as J2EE (Java 2 Enterprise Edition)-an expanded set of API libraries that provide special functions, such as servlets.