Table of ContentsJava's Growth in the SunMultiple Editions

So What Is Java?

Java is more than a programming language; it's a way of life. You should immediately terminate all other activities and devote your life entirely to the pursuit of perfection in your Java code. Not! But seriously, coding Java programs is a great way to kill a few hours. (I'm sad, I admit it, but heytell me you don't like coding too.)

Java is a little different than your typical programming language. First, most programming languages process code by either compiling or interpreting; Java does both. As you can see in Figure 1.3, the initial compile phase translates your source code (.java files) into an intermediate language called Java bytecode (.class files). The resulting bytecode is then ready to be executed (interpreted) within a special virtual computer known as the JVM (Java Virtual Machine).

Figure 1.3. Java code goes through two stages: compilation and interpretation.

graphic/01fig03.gif


The Java Virtual Machine is a simulated computer that executes all the bytecode instructions. It's through the JVM that Java gains its portability because it acts as a consistent layer between bytecode and the actual machine instructionsbytecode instructions are translated into machine-specific instructions by the JVM at runtime.

Compiled bytecode is the power behind Java's "Write Once, Run Anywhere" flexibility. As you can see in Figure 1.4, all a target platform needs is a JVM, and it has the power to execute Java applications, regardless of the platform on which they were originally compiled.

Figure 1.4. Java bytecode becomes a portable program executable on any Java Virtual Machine.

graphic/01fig04.gif


However, to make all this work successfully, you need more than just a programming languageyou need a programming platform. The Java platform is made up of three significant components:

  • The Java compiler and tools

  • The Java Virtual Machine

  • The Java API (Application Programming Interface)

NOTE

Java versus C++

Since most of us game programmers are C++ coders first, here's a brief rundown of the significant differences between Java and C++.

Thankfully, Java is based on C++; in fact, James Gosling implemented the initial versions of Java using C++. But James took the opportunity presented by Java's unique language model to modify the structure and make it more programmer-friendly.

The first major difference is memory management. Unlike in C++, in Java you basically aren't trusted enough to peek and poke at your own memorythat's the JVM's job. You can control the construction of objects, but you don't have any sort of fine-tuning control over the destruction of objects. You certainly can't do any of that fancy pointer arithmetic; there's just no concept of addressing an object in memory. However, the good news is that many of those hair-pulling, dangling-pointer, memory-scribbling, blue-screen nightmares just don't happen anymore.

The differences don't end there, though. Java also has the following notable variations from C++.

  • There is no preprocessing of source files in Java.

  • Unlike C++ there is no split between the interface (.h) and the implementation (.cpp). In Java there is only one source file.

  • Everything in Java is an object in the form of a class. (This also means there are no global variables.)

  • Java has no auto-casting of types; you have to be explicit.

  • Java has a simplified object model and patterns; there is no support for multiple inheritance, templates, or operator overloading.

As a C++ programmer, my first impression of Java was that it was a poor cousin. C++ gave me more control over execution and memory, and I missed my templates, preprocessor tricks, and operator overloading. However, after working with Java for some years, I honestly have to say I no longer mind the differences where performance is not critical. Java code generally performs far better and with fewer bugs. The Java API is a vast pool of free functionality, and the performance seems as good as or better than C++ in most circumstancesif for no other reason than because of the extra time I now spend improving my code.

The JVM's role is to provide an interface to the functionality of the underlying device, whereas the Java API provides a limited view of this functionality to the Java program. In this manner, the JVM is the judge, jury, and executor of program code.

The Java API is a collection of Java classes covering a vast range of functionality including containers, data management, communications, IO, security, and more. There are literally thousands of classes available as part of the Java platform.

    Table of ContentsJava's Growth in the SunMultiple Editions