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.)

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.)
  • 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.

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:
  1. 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.
  2. 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

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.
What Is Java, and Why Is It So Great? Reviewed by 1000sourcecodes on 08:33 Rating: 5
Powered by Blogger.